django – 如何在保存之前使用PIL调整新上传的图像大小?

我想调整高度和宽度为800px的新图像大小,并保存.而应用程序不能存储真实的图像.任何帮助?

这是我的代码,它保存原始图像,不要调整大小的照片:

models.py:

class Photo(models.Model):        
    photo = models.ImageField(upload_to='photos/default/')


    def save(self):

        if not self.id and not self.photo:
            return            

        super(Photo,self).save()

        image = Image.open(self.photo)
        (width,height) = image.size

        "Max width and height 800"        
        if (800 / width < 800 / height):
            factor = 800 / height
        else:
            factor = 800 / width

        size = ( width / factor,height / factor)
        image.resize(size,Image.ANTIALIAS)
        image.save(self.photo.path)

解决方法

image = image.resize(size,Image.ANTIALIAS)

调整大小是非破坏性的,它返回一个新的图像.

dawei

【声明】:淮南站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。