【发布时间】: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