MITB Banner

Hands-On Guide To Pillow – Python Library for Image Processing

Pillow offers all types of image processing capabilities like image transformation, rotation, resizing, statistics of the image, etc. It supports the extensive file format and is designed to fast access the data stored in pixels.

Share

Pillow - Python Library for Image Processing

Image processing is performing operations on an image to enhance it or to extract some useful information and to know about the attributes of the image. A systematic study of the image to find out some useful information about that image is known as image processing.

Python allows image processing using different libraries and one of them is Pillow, which is an open-source Python Imaging Library that adds image processing capabilities to your Python interpreter. 

Pillow offers all types of capabilities like image transformation, rotation, resizing, statistics of the image, etc. It supports the extensive file format and is designed to fast access the data stored in pixels. 

In this article, we explore Pillow and learn about its image processing capabilities.

Implementation:

Like any other library, we will first install pillow using pip install pillow

  1. Importing required library

We will start by importing Image function from PIL. This function is used to process the image and perform operations on it.

from PIL import Image

  1. Loading the image

We can work on any image of our choice of any format. I am using an image of a bird that I downloaded from google. 

img = Image.open("sparrow.jpg")

img

  1. Image processing

Lets us start the image processing by knowing the mode, size, and the format of the image. 

  1. Basic Properties

Pillow has inbuilt functions to know the basic properties of the image. Lets us see how we use them to draw some information.

print('Format:',img.format)

print('Size',img.size)

print('Mode:', img.mode)

  1. Cropping the Image

In the cropping section, we will crop the image by creating a box of a particular size and cut it out of the image. 

box1 = (200, 200, 800, 800)

crop1 = img.crop(box1)

crop

As you can see according to the size of the box we have a cut out from the image which is the cropped version.

  1. Splitting the Image

As we already know from above that the image is in RGB mode, in this step we will split it into ‘L’ mode i.e. a single channel Luminance.

R, G, B = img.split()

R

The image is split into three parts with a single channel ‘L’.

  1. Transformations

Let us analyze different transformation functions. Starting with resizing the image.

  •        Resizing 

resized_image = img.resize((256, 256))

resized_image

  •    Rotating Image

rotated_image = img.rotate(90)

rotated_image

Rotated Image
  •     Transposing Image

tran_image = img.transpose(Image.FLIP_TOP_BOTTOM)

tran_image

Transposed Image
  •      Changing color

img1 = im.convert('1')

img1

Black And White Image
  1. Filters

Let us analyze different filters that are defined under the Pillow library.

  • Blur Image

from PIL import ImageFilter

blurred = img.filter(ImageFilter.BLUR)

blurred

Blurred Image
  • Sharpen Image

sharped= img.filter(ImageFilter.BLUR)

sharped

  • Finding Edges

edges = sharped.filter(ImageFilter.FIND_EDGES)

edges

  • Enhancing Edges

en_edges = sharped.filter(ImageFilter.EDGE_ENHANCE)

en_edges

  • Emboss Filter

emb = sharped.filter(ImageFilter.EMBOSS)

emb

This is just an introduction to the image processing capabilities of the Pillow. All these functions are helpful in the initial stage of image processing. There are many advanced functions that can be used at a later stage.

Conclusion:

In this article, we have seen the basic image processing capabilities of the Pillow. We saw how we can access different image attributes, how we can perform different operations like transpose, resize, etc. This is just an introduction to what all Pillow can do.

Share
Picture of Himanshu Sharma

Himanshu Sharma

An aspiring Data Scientist currently Pursuing MBA in Applied Data Science, with an Interest in the financial markets. I have experience in Data Analytics, Data Visualization, Machine Learning, Creating Dashboards and Writing articles related to Data Science.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Courses & Careers

Become a Certified Generative AI Engineer

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.