[Image-SIG] Help w/ resize

kcaza@cymbolic.com kcaza@cymbolic.com
Fri, 26 May 2000 15:02:51 -0700

Basically, you just need to find out the aspect ratio of the new file at 275
pixels wide, and resize it using that size.  It's pretty simple.

import Image

def BetterThumbnail(im, xmax, ymax):
     inaspect =float(im.size[0])/ float(im.size[1])
     outaspect = float(xmax) / float(ymax)

     if inaspect > outaspect: # meaning that xmax will be used in resizing, and
y will be determined by aspect ratio
          im = im.resize((xmax, int((float(xmax) / inaspect) + 0.5)),
Image.BICUBIC) # round it off, resize with Bicubic
     else: # meaning that ymax will be used in resizing, and x will be
determined by aspect ratio
          im = im.resize((int((float(ymax) * inaspect) + 0.5), ymax),

     return im


That should do it for ya... check it out with a couple test images though, it's
similar to what I use in my stuff, but not exactly the same.


Jason Long <jason@streetrodstuff.com> on 05/15/2000 07:14:43 AM

To:   image-sig@python.org
cc:    (bcc: Kevin Cazabon/cymbolic/gig)

Subject:  [Image-SIG] Help w/ resize

Hi, I am a newbie to the PIL library and I need a little help.

For the record, I am using Python/PIL on Redhat 6.1 - Python version 1.5.2.

Here is my scenario:  I have some images of all different sizes that I would
like to resize to a size of 275 pixels wide, and I don't care how tall.  I want
to preserve the aspect ratio, though.  I have had success with the thumbnail()
function, however the image quality is not very good.  I was thinking that I
would get better results with resize() since I could specify BILINEAR or BICUBIC
filters.  I guess I am not exactly sure how to use the resize function correctly
for my needs.

To summarize, I need to:
- resize images of different sizes to (275, x)
- generate the best quality I can

Basically, I'd like the thumbnail functionality with better output quality.  I'm
sure I'm simply not doing something correctly.  Perhaps a snipet of code would
do me best.

Thank you very much in advance.


Jason Long
Webmaster http://www.streetrodstuff.com

Image-SIG maillist  -  Image-SIG@python.org