The Python Imaging Library or PIL is an amazing Python library used for image processing. This library provides so many features for working on images using Python. It is used as an image processing tool with other Python image processing libraries like OpenCV. If you’ve never used the Python Imaging Library (PIL) before, this article is for you. In this article, I will introduce you to a tutorial on the Python Imaging Library.
Python Imaging Library (PIL)
The PIL or Python Imaging Library is often confused with Pillow. Pillow is a fork of the PIL library in Python, that’s why to install PIL we write “pip install Pillow”, instead of “pip install PIL”. Some of the important features that this library offers you for image processing are:
- extensive file format support
- efficient internal representation
- creating thumbnails
- converting image files format
- applying filters to images
- also provides some powerful image processing capabilities
This Python library is already available in the Python standard library, but to use the latest version, I will recommend that you run the pip install command mentioned below before getting your hands on the Python Image Library:
- pip install Pillow
As mentioned earlier, it is used as PIL but installed as Pillow. Now in the section below, I will take you through a tutorial on the Python Imaging Library.
Python Imaging Library (PIL): Tutorial
I will start by reading and visualizing the image that I am using for this tutorial:
# Read and Display Image from PIL import Image image = Image.open("aman.png") image.show()

Now let’s see some of the details about the image:
# Image Details print(image.format) print(image.size) print(image.mode)
PNG (640, 640) RGB
Now let’s crop this image using random dimensions:
# Crop the Image dimensions = (100, 100, 500, 500) cropped = image.crop(dimensions) cropped.show()

Now here is how you can resize this image:
# Resize Image new = image.resize((200, 200)) new.show()

Rotating an image is also an amazing feature provided by this library. Here’s how to rotate an image:
# Rotate Image new = image.rotate(60) new.show()

The most helpful feature that I like about this library is that you can add filters to your image in just a few lines of code. It has some inbuilt filters that you can apply to your image. For example, have a look at the gaussian blur filter that I have applied to the image below:
# Adding Filter from PIL import ImageFilter new = image.filter(ImageFilter.GaussianBlur) new.show()

So this is how you can use the PIL or Python Imaging Library to work on your image datasets. You can explore more about this library from here.
Summary
The Python Imaging Library is an amazing Python library used for image processing. Some of the important features that this library offers you for image processing are:
- extensive file format support
- efficient internal representation
- creating thumbnails
- converting image files format
- applying filters to images
- also provides some powerful image processing capabilities
I hope you liked this article on a tutorial on Python Imaging Library. Feel free to ask your valuable questions in the comments section below.