How to resize images with python

08Jan11

I love Python,  and I’ve been learning it for a while now. Sometime ago I wrote a Python script where I needed to resize a bunch of images and at the same time keep the aspect ratio –  the proportions – intact. So I looked around and found the PIL (Python Imaging Library). You will need to install the PIL for the code to work. To install PIL on a Debian based system run the following command in your terminal as root:

aptitude install python-imaging

And now for the Python code for resizing images while maintaining the aspect ratio:

import PIL
from PIL import Image

basewidth = 300
img = Image.open(‘fullsized_image.jpg’)
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
img.save(‘resized_image.jpg’)

These few lines of Python code will resize an image (fullsized_image.jpg) using PIL to a width of 300 pixels, which is set in the variable basewidth and a height proportional to the new width.  The proportional height is calculated by determining what percentage 300 pixels is of the original width (img.size[0]) and then multiplying the original height (img.size[1]) by that percentage. The resulting height value is saved in the variable hsize.

You can change basewidth to any other number, if you need a different width for your images. Also, notice I saved the resized image under a different name - resized_image.jpg – because I wanted to preserve the full size image as well – fullsized_image.jpg. You don’t have to do this of course. You can use the same filename to overwrite the full size image with the resized image,  if that is what you want.

Note: If you are using this script in Zope as an External method you will need the line from PIL import Image to avoid namespace clashes with Zope’s Image.

UPDATE: avsn9849625263 Asked me to share what it would look like, if the height is fixed and the width proportionally variable. It’s pretty much the same thing, you just need to switch things around a bit:

baseheight = 560
img = Image.open(‘fullsized_image.jpg’)
hpercent = (baseheight / float(img.size[1]))
wsize = int((float(img.size[0]) * float(hpercent)))
img = img.resize((wsize, baseheight), PIL.Image.ANTIALIAS)
img.save(‘resized_image.jpg’)

Notice basewidth is now baseheight, since height is fixed. And in the third line we’re calculating the height percentage, so we need img.size[1] instead of img.size[0]. The size attribute is a tuple containing width and height in pixels; size[0] refers to the first tuple element, which is width, and size[1] is the second element, which is height. Line 4 also has this switch between size[0] for width and size[1] for height.

UPDATE 2: Kieran pointed out an error in l.5 in the im.resize() which has now been corrected. :)

About these ads


9 Responses to “How to resize images with python”

  1. 1 Flo

    nice post!
    here’s a blog-post about cropping+resizing images using PIL

    http://blog.lagentz.com/python/automatic-image-resizing-and-cropping-with-pylons/

  2. 2 vinay

    i am not able to open png files.
    what should be the location of the png file.it is showing files doesnot exists

  3. 5 msodrew

    thanks man. works great

  4. Nicely explained,

    Can you please share the logic to FIX height and calculating width

  5. Hi thanks for sharing this, great snippet.
    Was just using your version in the update for having Height as the baseline.
    In line 5 believe the baseheight, wsize need their position switched as well.
    So:
    img = img.resize((baseheight, wsize), PIL.Image.ANTIALIAS)
    Should be
    img = img.resize((wsize,baseheight), PIL.Image.ANTIALIAS)

    Thanks Kieran


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: