HOME/Articles/

pil example resize-image-keep-aspect-ratio (snippet)

Article Outline

Python pil example 'resize-image-keep-aspect-ratio'

Modules used in program:

  • import PIL

python resize-image-keep-aspect-ratio

Python pil example: resize-image-keep-aspect-ratio

#Resizes an image and keeps aspect ratio. Set mywidth to the desired with in pixels.

import PIL
from PIL import Image

mywidth = 300

img = Image.open('someimage.jpg')
wpercent = (mywidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((mywidth,hsize), PIL.Image.ANTIALIAS)
img.save('resized.jpg')