我想在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)
调整大小是非破坏性的,它返回一个新图像。
关于django - 保存前如何使用PIL调整新上传的图像的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7970637/