![](/img/trans.png)
[英]How can I resize an image without using .resize() in python PIL
[英]Python: How to resize an image using PIL module
我正在嘗試將圖像大小調整為500x500px但出現此錯誤:
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':
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
被認為是縮小規模的最佳選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.