[Image-SIG] PIL resize with aspect ratio?

Chris Cogdon chris at cogdon.org
Thu Jan 5 22:24:54 CET 2006

On Jan 5, 2006, at 12:17, Count László de Almásy wrote:

> Thanks.  How about setting the JPEG compression setting?
> Conceptually what I'm trying to do is the following:
> Take an image file of arbitrary size and of arbitrary width and height,
> and rescale it to a width of exactly 500 and a height of whatever,
> as long as the aspect ratio is preserved.  Additionally, the rescaled
> image must be < 100kb in size, but I want to preserve as much
> quality as possible.  My thought was that if I could specify the
> JPEG compression ratio as I can with ImageMagick, I could start
> at a high value and iteratively go lower and lower until a rescaled
> image file is produced that is just under 100kb size.

The thumbnail command just creates the thumbnail image in memory, but 
doesn't 'compress' it yet. You do the compression when you save it as a 
particular format, and then you can set the compression. here's a more 
complete example:

i = Image.open ( path )
i.thumbnail ( (128,128), Image.ANTIALIAS )
i.save ( thumb_path, quality=75 )

    ("`-/")_.-'"``-._        Chris Cogdon <chris at cogdon.org>
     . . `; -._    )-;-,_`)
    (v_,)'  _  )`-.\  ``-'
   _.- _..-_/ / ((.'
((,.-'   ((,/   fL

More information about the Image-SIG mailing list