【问题标题】:Cannot open jpg images with PIL or open()无法使用 PIL 或 open() 打开 jpg 图像
【发布时间】:2020-08-07 16:16:30
【问题描述】:

我正在测试将 ImageField 保存在 Django 中,但由于某种原因,我尝试过的所有 *.jpg 文件都无法使用,而我拥有的一个 png 可以使用。 在 WSL VCode 终端中使用 django shell。

python 3.7

django 3.0

枕头 7.1.2

如果我使用 open():

# this has no error
f = open('loko.jpg', 'rb')

#if I do 
f.read()  # it shows only b'', like its empty

# this saves the field but the image is 0 bytes and cannot be open
object.image.save('name', File(f))

如果我使用 PIL:

from PIL import Image

# this shows error
# PIL.UnidentifiedImageError: cannot identify image file 'loko.jpg'
img = Image.open('loko.jpg')

使用我尝试过的 .png 图片,两种方法都很好用。

可能是什么问题?

【问题讨论】:

    标签: python django python-imaging-library


    【解决方案1】:

    解决方案是更新 python-pillow 包。

    现在可以了。

    【讨论】:

      猜你喜欢
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      • 2014-11-22
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      相关资源
      最近更新 更多