Python:如何使用PIL模块调整图像大小 [英] Python: How to resize an image using PIL module

查看:467
本文介绍了Python:如何使用PIL模块调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像尺寸调整为500x500px,但出现此错误:

I'm trying to resize an image to 500x500px but got this error:

File "C:\Python27\lib\site-packages\PIL\Image.py", line 1681, in save
     save_handler = SAVE[format.upper()] KeyError: 'JPG'

这是代码:

from PIL import Image
img = Image.open('car.jpg')
new_img = img.resize((500,500))
new_img.save('car_resized','jpg')

推荐答案

您需要将调用save函数的format参数设置为'JPEG':

You need to set the format parameter in your call to the save function to 'JPEG':

from PIL import Image
img = Image.open('car.jpg')
new_img = img.resize((500,500))
new_img.save("car_resized.jpg", "JPEG", optimize=True)

这篇关于Python:如何使用PIL模块调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆