【发布时间】:2013-03-01 19:06:26
【问题描述】:
Django 新手 :)
我正在通过包django-storages 使用S3 存储。当我通过管理员上传/更新新图像时,这似乎很完美。
models.py(图像字段)
image = models.ImageField(
upload_to=path_and_rename("profiles"),
height_field="image_height",
width_field="image_width",
null=True,
blank=True,
editable=True,
help_text="Profile Picture",
verbose_name="Profile Picture"
)
image_height = models.PositiveIntegerField(null=True, blank=True, editable=False, default="100")
image_width = models.PositiveIntegerField(null=True, blank=True, editable=False, default="100")
然后我决定在上传时调整图像大小,因此尝试在保存覆盖方法中添加以下代码...
def save(self, *args, **kwargs):
if not self.id and not self.image:
return
super(Profile, self).save(*args, **kwargs)
image = Image.open(self.image).seek(0)
(width, height) = image.size
size = ( 100, 100)
image = image.resize(size, Image.ANTIALIAS)
image.save(self.image.path)
这是问题所在,这给出了以下错误....
无法识别图片文件
然后我昨天在堆栈上发布了一个问题(我删除了)和一个链接到这个答案的用户Django PIL : IOError Cannot identify image file 我有点理解(因为图像尚未上传它还无法阅读) . 但我不确定这是我的问题!当我收到错误无法识别图像文件我可以看到原始文件实际上已上传到 S3 (当然没有调整大小)。
记住我是新手 谁能修改我的示例保存方法(并解释) 以解决此问题?即一种在上传时将新图像调整为 100x100 的方法?
非常感谢
【问题讨论】:
标签: django django-models