Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to resize an image using PIL module

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'

This is the code:

from PIL import Image
img = Image.open('car.jpg')
new_img = img.resize((500,500))
new_img.save('car_resized','jpg')
like image 550
Doromi Avatar asked Jun 04 '16 14:06

Doromi


1 Answers

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)
like image 121
AK47 Avatar answered Sep 29 '22 05:09

AK47