0 votes
1 view
in Python by (21.3k points)

Is there an obvious way to do this that I'm missing? I'm just trying to make thumbnails.

1 Answer

0 votes
by (52.8k points)

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)

...