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
size = 128, 128
for infile in sys.argv[1:]:
outfile = os.path.splitext(infile) + ".thumbnail"
if infile != outfile:
im = Image.open(infile)
im.thumbnail(size, Image.ANTIALIAS) im.save(outfile, "JPEG")
print("cannot create thumbnail for '%s'" % infile)