Resize images using Python

A quick explanation of how to resize images in Python while keeping the same aspect ratio.
No readers like this yet.
Python in a tree

Original image CC0 from Wikimedia Commons

I love Python, and I've been learning it for a while now. Some time ago, I wrote a Python script where I needed to resize a bunch of images while at the same time keeping the aspect ratio (the proportions) intact. So I looked around and found Pillow, a Python imaging library and "friendly fork" of an old library just called PIL. 

To install Pillow, use the pip module of Python:

$ python3 -m pip install Pillow

Scaling by width

Here's a basic script to resize an image using the Pillow module:

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), Image.ANTIALIAS)
img.save('resized_image.jpg')

These few lines of Python code resize an image (fullsized_image.jpg) using Pillow 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 (fullsized_image.jpg) as well. 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.

Scaling by height

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:

blog and republished under Creative Commons with permission.

This article was updated in January 2021 by the editor.

Tags
User profile image.
A Rwandan Nigerian sysadmin, and Linux and FOSS geek, living in Kigali, Rwanda. I specialize in Linux server administration. My favourite distros are Linux Mint and Gentoo. I like to code in Python and use Plone. I am currently teaching myself Django. I speak German fluently, and studied Computer Science at the University of Applied Sciences in Hamburg, Germany.

Contributors

3 Comments

More interesting is how to properly resize videos with Go, Perl, Ruby or Python.

Interesting. Really helpful.

Hmm. The terminal command used to install PIL didn't work on my Mac (although this command was intended for Debian systems). For anyone running Mac, you can install the "friendly fork" of the PIL called "Pillow" (compatible with Python 3.0+ as well) using this command: sudo pip install Pillow
Perhaps we on Mac OSX are supposed to use this? :
apt-get install python-imaging

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.