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

Norman Khine norman at khine.net
Thu Oct 11 10:35:25 CEST 2007

Please excuse me, I am new to PIL, and was wondering which is the best 
method to do the following.

I have an application where users upload an image, at the moment if the 
image is over a certain size, it returns with an error asking the user 
to correct this.

I would like for my application to make the necessary adjustments to the 
image, but am unsure as to how to handle the portrait vs landscape image 

The image box, for example is 250x250.
  So from the example on 
http://www.pythonware.com/library/pil/handbook/image.htm I will have 
something like:

from PIL import Image
import glob, os

size = 250, 250

for infile in glob.glob("*.jpg"):
     file, ext = os.path.splitext(infile)
     im = Image.open(infile)
     im.thumbnail(size, Image.ANTIALIAS)
     im.save(file + ".thumbnail", "JPEG")

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



%>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) 
for c in ",adym,*)&uzq^zqf" ] )

More information about the Image-SIG mailing list