Table of contents
1.
Introduction 
2.
Using Pillow
2.1.
Import 
2.2.
Loading an Image
2.3.
Getting the properties of an Image
2.4.
Cropping the image
2.5.
Splitting an Image
2.6.
Transformations in Image
2.7.
Filters In Image
3.
Frequently Asked Questions
4.
Conclusion
Last Updated: Aug 13, 2025
Easy

Handling Images with Pillow Module in Python

Introduction 

Handling images is a widespread use case in programming. There are many ways to take pictures, and we are going to learn how to handle images with the Pilow module in Python. It's a viral module for image handling, and we will see how we can use various functionalities of this module to achieve our use cases. 

For this, please make sure that you have Pillow installed in your system.

pip install pillow

The above command can be used to install this module. 

Must Recommended Topic, Floor Division in Python, Swapcase in Python

Using Pillow

Import 

from PIL import Image
You can also try this code with Online Python Compiler
Run Code

Loading an Image

img=Image.open(“/path/to/image.jpg”)
img.show()
You can also try this code with Online Python Compiler
Run Code

It will show the image. 

img object returned is our go-to object to make any changes in the image. We will interact with this img object and call functions to achieve our use-cases, which can be many. 

Also read, Convert String to List Python

Getting the properties of an Image

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

print('Format:',img.format)
print('Size',img.size)
print('Mode:', img.mode)
You can also try this code with Online Python Compiler
Run Code

There are various modes in which images can be there; below is a description of multiple ways of an image. 

The format can be png, jpeg, jpg etc.  

Also Read About, Python for Data Science

Cropping the image

While cropping an image we need to tell how much we need to cut from each corner; for that we provide a tuple of length four while cropping an image. 

cropped = img.crop((1,2,300,300)) # tuple for cropping the image
#Display the cropped portion
cropped.show()
#Save the cropped image
cropped.save('images/croppedBeach1.jpg')
You can also try this code with Online Python Compiler
Run Code

img.crop is returning the new cropped image. 

Splitting an Image

The image can be split into channels of which it is made of; using the split function, it returns a tuple having 3 values, one for each channel. 

R, G, B= img.split()
You can also try this code with Online Python Compiler
Run Code

Also Read - Image Sampling

Transformations in Image

Here we will discuss how can we apply various transformations to an Image, like rotation, resizing etc. 

Resizing

resized_img=img.resize((200,200))
You can also try this code with Online Python Compiler
Run Code

Rotating an Image

rotated_image = img.rotate(90)
You can also try this code with Online Python Compiler
Run Code

Transposing Image

tran_image = img.transpose(Image.FLIP_TOP_BOTTOM)
You can also try this code with Online Python Compiler
Run Code

there are other types of transformations, like Image.FLIP_LEFT_RIGHT. 

Changing the Color

from PIL import Image
# creating image object
img = Image.open('img.jpg')
img.show()  
You can also try this code with Online Python Compiler
Run Code

This is our input image. 

# using convert method for img1
img1 = img.convert("L")
img1.show()
You can also try this code with Online Python Compiler
Run Code
# using convert method for img2
img2 = img.convert("1")
img2.show()
You can also try this code with Online Python Compiler
Run Code

Filters In Image

from PIL import ImageFilter
from PIL import Image
img=Image.open('img.jpg')
# blurring
blurred = img.filter(ImageFilter.BLUR)
blurred.show()
# sharpening the edges
sharped= img.filter(ImageFilter.SHARPEN)
sharped.show()
#Finding Edges
edges = sharped.filter(ImageFilter.FIND_EDGES)
edges.show()
#Enhancing Edges
en_edges = sharped.filter(ImageFilter.EDGE_ENHANCE)
en_edges.show()
#Emboss Filter
emb = sharped.filter(ImageFilter.EMBOSS)
emb.show()
You can also try this code with Online Python Compiler
Run Code

Output: 

You can compile it with online python compiler.

Frequently Asked Questions

1. What is the pillow module in Python?
Python Imaging Library (expansion of PIL) is the de facto image processing package for Python language. It incorporates lightweight image processing tools that aid in editing, creating and saving images.
2. Does PIL work in Python 3?
The "friendly PIL fork" Pillow works on Python 2 and 3.
3. What are some alternatives to Pillow?
Open CV, scikit-image etc., are some of the alternatives to the Pillow module. 
4. What does cv mean in Python?
Essentially, the word refers to providing a computer with the ability to observe the world in the same way that people do. Computer vision is a branch of computer science that allows computers to mimic the human visual system.

Conclusion

In a nutshell, PIllow is a very powerful module to play around with images there are many functionalities that can be used for a variety of purposes of image handing in python with Pillow. 

Check out this article - Quicksort Python

Hey Ninjas! Don't stop here; check out Coding Ninjas for Machine Learning, more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. 

Happy Learning!

Live masterclass