Pillow Python Tutorial in Details

Pillow Python Tutorial in Details

Python is an amazing language not just because it is easy to learn but it has lots of libraries and tools that make the job easy and cool. One such tool in python is Pillow.

In Python, Pillow is one of the most popular tools to deal with digital images. Of course, we have other tools in python too like OpenCV, Scikit-image, PIL, etc. that do the same thing.

Pillow is a Python Imaging Library (PIL) fork, which adds support for opening, manipulating, and saving many different image file formats. It provides a wide range of image processing capabilities, such as image filtering, image enhancement, and image transformation, making it a popular tool for working with images in Python.

Why do we use Pillow?

After the discontinuation of the tool Python Imaging Library (PIL) in 2011, it took the form of Pillow with added features and support of Python3.x support.

The pillow is a lightweight image processing tool that helps in editing, modifying, and saving images in Python. It supports a large number of image file formats including PNG, JPEG, BMP, and TIFF.

Pillow can be used for a variety of tasks, including:

  1. Image opening and saving: Pillow can open and save images in a variety of formats, such as JPEG, PNG, GIF, BMP, and many others.
  2. Image manipulation: Pillow provides a range of image manipulation features, such as cropping, resizing, rotating, and flipping images.
  3. Image filtering: Pillow supports many different types of image filters, such as blurring, sharpening, edge detection, and color manipulation.
  4. Image enhancement: Pillow provides tools for enhancing images, such as adjusting brightness and contrast, and color balance.
  5. Image generation: Pillow can be used to generate images programmatically, such as creating geometric shapes, gradients, and textures.

Pillow Python Installation

If Python is already installed on your machine, you can now install Pillow just by pasting the following code into your terminal. If it is already installed, you might get the message “requirement already satisfied.

pip install pillow

Python Pillow Working Example: Image Processing

Calling an Image and showing it

Opening an image in Python using Pillow is just basic. You need to import the image class from the PIL library and open it by providing the image URL.

from PIL import Image
img = Image.open(r"filename.png") 
  • “r” here is the mode which means read
  • If the image is in some directory, you have to provide the full path

Getting the information about the image: Size, Color mode, Format

Sometimes we need to get information about the images. We can get it simply in Pillow using the below lines of Python codes.

from PIL import Image
img = Image.open(r"test.png")
#This will return image format
#This will return image dimension
#This will return image color mode
(300, 300)

Rotating and image with the certain degree in pillow

Sometimes we need to deal with rotating an image for processing. This can be easily done with a simple few lines of code in python. Below is the code snippet.

from PIL import Image

img = Image.open(r"filename.png")
rotated_img = img.rotate(45)

Resizing an image in pillow python

Image resizing is an important part of image processing. You need to resize an image frequently in python while making any projects especially related to Deep Learning where you have lots of images. It helps you to have all the images in a single size and also to reduce the pixels of images so that it would become computationally less expensive to deal with tons of images. Below is the code which gets your image resized in Python.

from PIL import Image
img = Image.open(r"test.png")
r_img = img.resize(64, 64)

Writing something on the image in pillow python

Sometimes you feel the need to write on an image, either as an image title, or desc, or you want to write as a watermark on the image. You can also do this simply using Pillow in python as stated below.

from PIL import Image, ImageDraw 
img = Image.open(r'filename.jpg')  
d1 = ImageDraw.Draw(img)  
d1.text((32, 44), "Hello", fill=(255, 255, 0))  

Blurring an image

Sometimes you feel the requirement to blur the image. Actually blurring an image makes it less noisy, and smooths it edges.

There are 3 different types of blur: Simple BlurBox Blur, and Gaussian Blur.

Simple blur is a very basic blurring technique that simply averages the pixel values in a small window around each pixel. This produces a slightly blurred version of the original image.

Box blur is a slightly more sophisticated blurring technique that uses a box-shaped kernel to average the pixel values in a small window around each pixel. This produces a slightly more blurred version of the original image than a simple blur.

Gaussian blur is a more advanced blurring technique that uses a Gaussian kernel to average the pixel values in a small window around each pixel. This produces a more naturally blurred version of the original image and is often used in image processing and computer vision applications.

from PIL import Image, ImageFilter  
Original_Image = Image.open(r"filename.jpg")  
#Applying Simple blur function  
blur_Image = Original_Image.filter(ImageFilter.BLUR)  

#Applying Box blur function  
blur_Image = Original_Image.filter(ImageFilter.BoxBlur(radius))  
#Radius can be from 0 to 1

#Applying Gaussian blur
blur_Image = Original_Image.filter(ImageFilter.GaussianBlur(6))  

Flipping an image in pillow

Flipping an image is a very necessary requirement when you are dealing with certain types of projects, especially for Deep Learning. It helps you to generate more data from a single image.

There can be 3 types of image flip:

  • ROTATE_90

Below is the code for FLIP-LEFT_RIGHT

from PIL import Image  
imageObject = Image.open(r'filename.png')  
#Perform a flip of left and right  
flipImg = imageObject.transpose(Image.FLIP_LEFT_RIGHT)  

Saving an Image in Pillow

After doing image processing in Python Pillow, you need to save the resultant image. This can be easily done again in Pillow with the below line of codes.


Here we have wanted to save our image as the resultant name with a png extension, so the above is used. You can save it in any extension you want.

Creating an image in Pillow Python

To create a new image using Python Pillow PIL library, use Image.new() method.

In this example, we will create a new image with RGB mode, (400, 300) as size and a color of (209, 123, 193) corresponding to Red, Green and Blue channels respectively

from PIL import Image  

width = 400
height = 300

img  = Image.new( mode = "RGB", size = (width, height), color = (209, 123, 193) )


Pillow is widely used in web development, scientific research, and data analysis, among other fields. It is compatible with a variety of Python versions and can be installed using pip, the Python package manager. The official Pillow documentation provides a detailed reference on how to install and use the library.