Python: 如何使用 PIL 模块调整图像大小
Python: How to resize an image using PIL module
我正在尝试将图像的大小调整为 500x500 像素,但出现此错误:
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')
您需要在调用保存函数时将格式参数设置为'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)
解决方法如下:
from PIL import Image
img = Image.open('car.jpg')
new_img = img.resize((500,500), Image.ANTIALIAS)
quality_val = 90 ##you can vary it considering the tradeoff for quality vs performance
new_img.save("car_resized.jpg", "JPEG", quality=quality_val)
PIL 中有重采样技术列表,如 ANTIALIAS
、BICUBIC
、BILINEAR
和 CUBIC
。
ANTIALIAS
被认为是缩小规模的最佳选择。
我正在尝试将图像的大小调整为 500x500 像素,但出现此错误:
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')
您需要在调用保存函数时将格式参数设置为'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)
解决方法如下:
from PIL import Image
img = Image.open('car.jpg')
new_img = img.resize((500,500), Image.ANTIALIAS)
quality_val = 90 ##you can vary it considering the tradeoff for quality vs performance
new_img.save("car_resized.jpg", "JPEG", quality=quality_val)
PIL 中有重采样技术列表,如 ANTIALIAS
、BICUBIC
、BILINEAR
和 CUBIC
。
ANTIALIAS
被认为是缩小规模的最佳选择。