django – 如何在保存之前使用PIL调整新上传的图像大小?
发布时间:2020-12-16 23:37:04 所属栏目:Python 来源:网络整理
导读:我想调整高度和宽度为800px的新图像大小,并保存.而应用程序不能存储真实的图像.任何帮助? 这是我的代码,它保存原始图像,不要调整大小的照片: models.py: class Photo(models.Model): photo = models.ImageField(upload_to='photos/default/') def save(se
我想调整高度和宽度为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) 调整大小是非破坏性的,它返回一个新的图像. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |