To resize an image using PIL and maintain its aspect ratio you need to define a maximum size. After defining the maximum size you need to compute a resize ratio by using the following formula which is min(maxwidth/width, maxheight/height).
Below is the code example which will guide you on how to write the code for resizing the image using PIL.
import os, sys
import Image
size = 128, 128
for infile in sys.argv[1:]:
outfile = os.path.splitext(infile)[0] + ".thumbnail"
if infile != outfile:
try:
im = Image.open(infile)
im.thumbnail(size, Image.ANTIALIAS) im.save(outfile, "JPEG")
except IOError:
print("cannot create thumbnail for '%s'" % infile)