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:  http://www.pythonware.com/products/pil/index.htm
# 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 = Image.open(imageFile)

# 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"
im2.save("NEAREST" + ext)
im3.save("BILINEAR" + ext)
im4.save("BICUBIC" + ext)
im5.save("ANTIALIAS" + ext)

# optional image viewer ...
# image viewer  i_view32.exe   free download from:  http://www.irfanview.com/
# avoids the many huge bitmap files generated by PIL's show()
import os
os.system("d:/python24/i_view32.exe %s" % "BILINEAR.jpg")
11 Years
Discussion Span
Last Post by Budy_1

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


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.


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 = Image.open(image_file)
# 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 #

Edited by krystosan: typo

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\Image.py", line 1916, in open
     raise IOError("cannot identify image file")
 IOError: cannot identify image file 

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


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


Edited by Budy_1: typo

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.