我想调整高度和宽度为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)
调整大小是非破坏性的,它返回一个新的图像.