ben*_*eni 16 django django-models python-imaging-library
我想以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)
Run Code Online (Sandbox Code Playgroud)
wmi*_*mil 11
image = image.resize(size, Image.ANTIALIAS)
Run Code Online (Sandbox Code Playgroud)
调整大小是非破坏性的,它会返回一个新图像.
归档时间: |
|
查看次数: |
19052 次 |
最近记录: |