为什么这种保存方法不会调整图像大小?

时间:2015-01-06 02:55:55

标签: django-models django-forms

我重写了save方法,以便在上传后调整图像大小。以下代码似乎没有调整图像大小,我仔细检查了媒体文件夹,并且上传图像只有一个副本的原始尺寸(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)

我做错了什么?

0 个答案:

没有答案