Using the Python Image Library (PIL) you can resize an image. Several filters can be specified. ANTIALIAS is best for downsampling, the other filters work better with upsampling (increasing the size). In this code snippet one image of each filter option is saved, so you can compare the quality in your favorite image viewer. Also an alternative to the PIL show() function is given. This way you can avoid the clutter of bitmap files generated by PIL and left behind in your \Local Settings\Temp folder.

# resize an image using the PIL image library
# free from:
# tested with Python24        vegaseat     11oct2005

import Image

# open an image file (.bmp,.jpg,.png,.gif) you have in the working folder
imageFile = "zFlowers.jpg"

im1 =

# adjust width and height to your needs
width = 500
height = 420
# use one of these filter options to resize the image
im2 = im1.resize((width, height), Image.NEAREST)      # use nearest neighbour
im3 = im1.resize((width, height), Image.BILINEAR)     # linear interpolation in a 2x2 environment
im4 = im1.resize((width, height), Image.BICUBIC)      # cubic spline interpolation in a 4x4 environment
im5 = im1.resize((width, height), Image.ANTIALIAS)    # best down-sizing filter

ext = ".jpg""NEAREST" + ext)"BILINEAR" + ext)"BICUBIC" + ext)"ANTIALIAS" + ext)

# optional image viewer ...
# image viewer  i_view32.exe   free download from:
# avoids the many huge bitmap files generated by PIL's show()
import os
os.system("d:/python24/i_view32.exe %s" % "BILINEAR.jpg")
About the Author
Member Avatar vegaseat DaniWeb's Hypocrite


Irfanview was created by Irfan Skiljan when he studied at the TH-Wien in Austria. My favorite image viewer!

Your result image dont match the original aspect ratio! the thumbnail method has better performance than resize ...

look at for a more complete code snipplet!

Thank you very much for your truly smarter algorithm.

there's a nice post about cropping images and keeping the ascpect ratio here:

For anyone else having a problem with 'resize': I don't know why but it doesn't seem to work for me. It doesn't return an error but when I check the file, nothing has changed on it. I tried 'thumbnail' and this worked.

HiHe 174

This seems to work:

# using the Python Image Library (PIL) to resize an image
# works with Python27 and Python32

from PIL import Image
import os

image_file = "Flowers.jpg"

img_org =
# get the size of the original image
width_org, height_org = img_org.size

# set the resizing factor so the aspect ratio can be retained
# factor > 1.0 increases size
# factor < 1.0 decreases size
factor = 0.75
width = int(width_org * factor)
height = int(height_org * factor)

# best down-sizing filter
img_anti = img_org.resize((width, height), Image.ANTIALIAS)

# split image filename into name and extension
name, ext = os.path.splitext(image_file)
# create a new file name for saving the result
new_image_file = "%s%s%s" % (name, str(factor), ext)
print("resized file saved as %s" % new_image_file)

# one way to show the image is to activate
# the default viewer associated with the image type
import webbrowser

The original code was used to look at the quality of the available resize filters. So thanks to all the coders that added the aspect ratio option, or a way to speed things up..

when I try to resize and save with above mentioned methods i get error saying # IOError: cannot identify image file #

Please give us your full Traceback Error Message.

Error: line 1: cannot identify image file
 Traceback (most recent call last):
  File "<maya console>", line 5, in <module>
   File "E:\Program Files\Autodesk\Maya2013\Python\", line 1916, in open
     raise IOError("cannot identify image file")
 IOError: cannot identify image file 

Do you have PIL (Python Image Library) installed?

Yes ,but I am writing this code for Autodesk Maya software, and when I try to run PIL Maya crashes.

Thanks * 2**20 ... solved my query instantly!

I wrote this to resize Thousands of images in a single command run
Click Here <--link doesnt work ??