django – 如何在保存之前使用PIL调整新上传的图像大小?
发布时间:2020-10-19 21:24:45 所属栏目:Python 来源:互联网
导读:我想调整高度和宽度为800px的新图像大小,并保存.而应用程序不能存储真实的图像.任何帮助? 这是我的代码,它保存原始图像,不要调整大小的照片: models.py: class Photo(models.Model): photo = models.ImageField(upload_to=photos/default/) def save(self)
|
我想调整高度和宽度为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) 调整大小是非破坏性的,它返回一个新的图像. (编辑:鲜蔬坊站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- import next()python 2.5
- 【Python】用Python写脚本,完全备份和增量备份
- python – gcloud.exceptions.Forbidden:403权限丢失或不足
- 【PYthon】os.path.splitext()与os.path.split()的区别
- python – 关于Pandas Dataframe的Kurtosis doent工作
- python – 使用dict参数的带有OR条件的Django过滤器
- Python面向对象基础:编码细节和注意事项!
- python – 找不到manage.py collectstatic命令,Django 1.5.
- python基础 面向对象编程
- python-2.7 – 无法安装PythonMagick Windows 7
