Is there an obvious way to do this that I'm missing? I'm just trying to make thumbnails.
Define a maximum size.
Then, compute a resize ratio by taking
The proper size is
There is of course also a library method to do this: the method
Below is an (edited) example from the PIL documentation.
import os, sys import Image size = 128, 128 for infile in sys.argv[1:]: outfile = os.path.splitext(infile) + ".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
This script will resize an image (somepic.jpg) using PIL (Python Imaging Library) to a width of 300 pixels and a height proportional to the new width. It does this by determining what percentage 300 pixels is of the original width (img.size) and then multiplying the original height (img.size) by that percentage. Change "basewidth" to any other number to change the default width of your images.
from PIL import Image basewidth = 300 img = Image.open('somepic.jpg') wpercent = (basewidth/float(img.size)) hsize = int((float(img.size)*float(wpercent))) img = img.resize((basewidth,hsize), Image.ANTIALIAS) img.save('sompic.jpg')