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

Fredrik Lundh fredrik at pythonware.com
Fri Oct 12 22:08:47 CEST 2007


Norman Khine wrote:

> 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

Only if you apply it on a square image.

> 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

Not sure how you're rounding things here, but PIL's thumbnail method 
does indeed preserve the aspect ratio:

 >>> from PIL import Image
 >>> im = Image.new("RGB", (500, 635))
 >>> im.size
(500, 635)
 >>> im.thumbnail((250, 250))
 >>> im.size
(197, 250)

 >>> im = Image.new("RGB", (625, 500))
 >>> im.size
(625, 500)
 >>> im.thumbnail((250, 250))
 >>> im.size
(250, 200)
 >>>

</F>



More information about the Image-SIG mailing list