How To Resize Images Using Python


One task I use Python to perform on my desktop is image manipulation. Although there are plenty of good image processor utilities (i.e. IrfanView and XnView) sometimes it is simply quicker to create a Python script to complete a particular task for you.

By importing the Image library and making use of the resize function images can be resized to a specified size. It supports all common image formats such as PNG, GIF, JPG and BMP.

The basics of image resizing are shown below :

# Import the PIL image library
import Image

# Import os library for file manipulation functions
import os

# Open an image file (.bmp,.jpg,.png,.gif)
myimagefile = "myphoto.jpg"
myimage =

# Set new width and height
width_new = 800
height_new = 600

# Choose resize filter to use.
filter = Image.ANTIALIAS

# Resize the image
newimage = myimage.resize((width_new, height_new), filter)

# Split filename
filename, extension = os.path.splitext(myimagefile)

# Save the image + "_resized" + extension)

The other filters available when resizing are :

  • Image.NEAREST
  • Image.BILINEAR
  • Image.BICUBIC

Image.ANTIALIAS works fine for me so I tend to use that for resizing images.

The “” function saves the file in the format determined by the file extension. In this case JPEG.

This example can be used as the basis for a script that resizes a directory full of images. It can be formed into a function which can be called within a loop that acts on each image in turn.


Leave A Reply