Want to resize an image using PIL and maintain its aspect ratio? Define a maximum size. Then, compute a resize ratio by taking min(maxwidth/width, maxheight/height). The proper size is
There is of course also a library method to do this: the method Image.thumbnail. Below is an (edited) example from the PIL documentation:
import os, sys
size = 128, 128
for infile in sys.argv[1:]:
outfile = os.path.splitext(infile) + ".thumbnail"
if infile != outfile:
im = Image.open(infile)
print "cannot create thumbnail for '%s'" % infile
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.