有没有办法在Python中强制调整图像大小到指定尺寸?

3

我正在使用pillow库。我有一个尺寸为1280x1920的图像。当我上传这张图片时,我想将其调整为800x600的尺寸。但是上传后,图片被调整到了475x350的尺寸。是否有办法强制将图片调整为给定的尺寸?以下是我在Django中调整图片大小的代码:

img = Image.open(self.image.path)

if img.width > 800 or img.height > 600:
    img.resize((800, 600), Image.ANTIALIAS)
    img.save(self.image.path)
1个回答

2
resize() 方法返回修改后的图像的 副本 - 它不会直接修改传入的图像,因此您需要:
img = Image.open(self.image.path)

if img.width > 800 or img.height > 600:
    img = img.resize((800, 600), Image.ANTIALIAS)     # save result of resize()
    img.save(self.image.path)

谢谢您的回答。问题现在已经解决了。 - Wasek Samin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接