我重写保存方法以便在上传后调整图像大小。以下代码似乎并未调整图像大小,我仔细检查了媒体文件夹,发现上传图像的原始尺寸 (900x850) 只有一份副本。 Django 没有抛出任何错误,所以我不知道如何解决这个问题。需要明确的是,我可以毫无问题地提交表单和上传图像,但图像没有按照我的预期调整大小
class Mymodel(models.Model):
photo = models.ImageField(upload_to="...", blank=True)
def save(self, *args, **kwargs):
# Did we have to resize the image?
# We pop it to remove from kwargs when we pass these along
image_resized = kwargs.pop('image_resized',False)
if self.photo and image_resized:
basewidth = 300
filename = self.get_source_filename()
image = Image.open(filename)
wpercent = (basewidth/float(image.size[0]))
hsize = int((float(image.size[1])*float(wpercent)))
img = image.resize((basewidth,hsize), PIL.Image.ANTIALIAS)
self.photo = img
# Save the updated photo, but inform when we do that we
# have resized so we don't try and do it again.
self.save(image_resized = True)
super(Mymodel, self).save(*args, **kwargs)
我究竟做错了什么?
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)