Getting Started with Image Processing in Python using Pillow

Learn how to use the Pillow library in Python

Python has made every programmer’s life better but is it the truth? Let’s say we are building an eCommerce platform where anyone can sell their products. To reach more viewers, users will be uploading product images in any resolution, size, orientation, etc. Now, viewing images of different resolutions and sizes can be annoying for customers.

How can we solve this problem? We can use Pillow, an image processing library in Python.

We’ll explore the Pillow library in Python and its purpose. Then, we’ll go through the installation process and cover some basic operations to get started with Pillow.

Introduction to Pillow

Pillow is a powerful image processing library that is used to perform a wide range of operations on images, such as resizing, cropping, rotating, and much more.

It is a fork of the Python Imaging Library (PIL) which is a go-to library of Python to work with images, Unfortunately, it has not been updated for a long time and does not support Python 3. Therefore, Pillow is used as an alternative.

Pillow offers us a wide range of options when working with images and supports many popular image formats such as JPEG, PNG, GIF, and even less common formats such as TIFF and WebP. To use Pillow, we will need to first install it on our local PC using PIP.

Installation

PIP is a package manager which is used to install and manage packages written in Python. To install Pillow, open the command prompt or terminal and write the following command:

pip install pillow  

This will install the latest version of Pillow onto your PC. Now, let’s explore what we can do with Pillow.

Basic Image Operations

To work with an image, you first need to load and display it.

Load Images

First, we’ll need to import the Pillow library. Write the following line to import Pillow into your code.

from PIL import Image

Though the library is commonly called “Pillow,” the package name remains “PIL”. That’s why we are using from PIL import Image instead of from Pillow import Image. This line imports the image object, we’ll use it to load the image.

To load an image, we use the image.open(), which takes the file path as input and returns the Image object. We do this like so:

img = Image.open(<path to image>)

Replace <path to image> with the actual path of your image.jpg.

Display Images

After we’ve successfully loaded our image into the program, we can display it to visualize the results of our operations. To do this, we use the show() function like so:

# Display the loaded image
img.show()

This will display your image in the default image viewer of your operating system.

Save Images

Once you’ve processed an image and obtained the desired result, you’ll want to save it to the local storage for later use or sharing. Pillow makes it easy to save images in various file formats using the save() method of the Image object. Here’s how to save an image:

# Save the processed image to a file
img.save('processed_image.jpg')

Replace the processed_image.jpg with the desired file name and extension. The image is now saved to our machine.

Example

Let’s write a code to perform these operations. We’ll load a file in the code, display it, and save it with a different name.

from PIL import Image, ImageFilter
image = Image.open('codecademy.png')
image.save('modified.png')
image.show()

In the above program, we loaded an image file called ‘codecademy.png’ with the help of Image.open() and then saved it with a different name. We can see the output in the following image.

Output of the Python Code

Wrapping up

As we’ve seen, Pillow is a popular library in Python for image processing. Here are some key takeaways from this tutorial -

  • We can perform operations like resizing, cropping, and rotating with the help of Pillow.

  • It is a fork of the Python Imaging Library (PIL).

  • You can use Image.open() to load the images, show() to display, and save() to save your processed images on your device.

This was just a small example of what we can do with Pillow. Pillow is a powerful image library and can do much more intricate and complex image processing. Visit the reference doc to see what other functions are available.

Author

Codecademy Team

The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.

Meet the full team