【发布时间】:2011-05-30 14:29:11
【问题描述】:
在我的项目中将原始用户图像保存在userProfile 模型中时,我正在尝试创建和保存缩略图,下面是我的代码:
def save(self, *args, **kwargs):
super(UserProfile, self).save(*args, **kwargs)
THUMB_SIZE = 45, 45
image = Image.open(join(MEDIA_ROOT, self.headshot.name))
fn, ext = os.path.splitext(self.headshot.name)
image.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
thumb_fn = fn + '-thumb' + ext
tf = NamedTemporaryFile()
image.save(tf.name, 'JPEG')
self.headshot_thumb.save(thumb_fn, File(open(tf.name)), save=False)
tf.close()
super(UserProfile, self).save(*args, **kwargs)
一切正常,只有这一件事。
问题在于缩略图功能仅将宽度设置为45,并且不会更改图像的比例方面,因此我正在为我正在测试的图像获得45*35 的图像(短图像)。
谁能告诉我我做错了什么?如何强制我想要的宽高比?
P.S.:我已经尝试了所有尺寸的方法:tupal: THUMB_SIZE = (45, 45),并且还直接将尺寸输入到缩略图功能。
另一个问题:PIL 中的 resize 和 thumbnail 函数有什么区别?何时使用调整大小以及何时使用缩略图?
【问题讨论】:
标签: python django django-models python-imaging-library