Django如何确定上传的图像是否有效?

Bry*_*ock 3 python django python-imaging-library

我正在尝试在我的Django应用程序中为我的模型添加图像.

models.py

class ImageMain(models.Model):
"""This is the Main Image of the product"""
    product = models.ForeignKey(Product)
    photo = models.ImageField(upload_to='products')
Run Code Online (Sandbox Code Playgroud)

在开发模式下,每次我尝试通过Django管理员上传图像时,我都会得到:

Upload a valid image. The file you uploaded was either not an image or a corrupted image.
Run Code Online (Sandbox Code Playgroud)

我正在尝试上传的jpg可以通过os X Preview查看,因此它似乎是有效的.

似乎问题是Python Imaging Library没有将它识别为图像.为什么会出现有效图像?

PIL已安装,通过Django shell验证.

Nad*_*mli 12

根据Django的源代码.这三行负责验证图像:

from PIL import Image
trial_image = Image.open(file)
trial_image.verify()
Run Code Online (Sandbox Code Playgroud)

PIL可能不支持图像类型.在此处查看支持的格式列表