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

Norman Khine norman at khine.net
Sat Oct 13 09:29:10 CEST 2007


Thanks Fredrik.

Fredrik Lundh wrote:
> 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>
> 
> _______________________________________________
> Image-SIG maillist  -  Image-SIG at python.org
> http://mail.python.org/mailman/listinfo/image-sig
> 

-- 
Norman Khine

%>>> "".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