Monthly Archives: August 2012

Image resize to portrait height or landscape width using Python & PIL

from PIL import Image

filename = "car_.jpg"
im = Image.open(filename)

ratio = float(im.size[0]) / float(im.size[1])
print ratio
if ratio > 1:
	target_size=(500, int(500/ratio))
else:
	target_size=(int(500*ratio),500)
im2 = im.resize((target_size), Image.NEAREST)  # use nearest neighbour
im2.save("resized/"+filename)
print filename + "resized to " + repr(target_size) + " and saved."
Advertisements