加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 12:36:22 所属栏目:Python 来源:网络整理
导读:我正在尝试在我的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') 在开发模式下,每次
我正在尝试在我的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')

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

Upload a valid image. The file you uploaded was either not an image or a corrupted image.

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

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

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

解决方法

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

from PIL import Image
trial_image = Image.open(file)
trial_image.verify()

PIL可能不支持图像类型.检查支持的格式列表here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读