[Image-SIG] PIL scale attributes to fit 250x250 max

Randolph Bentson bentson at holmsjoen.com
Thu Oct 11 14:31:01 CEST 2007

On Thu, Oct 11, 2007 at 10:35:25AM +0200, Norman Khine wrote:
> But this creates a 250x250 thumbnail, whereas I would like if the image 
> is say 500x635 in size to reduce this to 196x250 and the same for my 
> portrait image if this is 625x500 to reduce this to 250x196

How about the following:

from PIL import Image
import glob, os

max_dimension = 250

for infile in glob.glob("*.jpg"):
     file, ext = os.path.splitext(infile)
     im = Image.open(infile)
     larger = max(im.size)
     size = im.size[0]*max_dimension/larger, im.size[1]*max_dimension/larger
     im.thumbnail(size, Image.ANTIALIAS)
     im.save(file + ".thumbnail", "JPEG")

Randolph Bentson
bentson at holmsjoen.com

More information about the Image-SIG mailing list