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.
Subscribe to our Newsletter
Join our editors every weekday evening as they steer you through the most significant news of the day, introduce you to fresh perspectives, and provide unexpected moments of joy
In this article, we explore Pillow and learn about its image processing capabilities.
Like any other library, we will first install pillow using pip install pillow
- 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
- 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")
- Image processing
Lets us start the image processing by knowing the mode, size, and the format of the image.
- 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.
- 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)
As you can see according to the size of the box we have a cut out from the image which is the cropped version.
- 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()
The image is split into three parts with a single channel ‘L’.
Let us analyze different transformation functions. Starting with resizing the image.
resized_image = img.resize((256, 256))
- Rotating Image
rotated_image = img.rotate(90)
- Transposing Image
tran_image = img.transpose(Image.FLIP_TOP_BOTTOM)
- Changing color
img1 = im.convert('1')
Let us analyze different filters that are defined under the Pillow library.
- Blur Image
from PIL import ImageFilter
blurred = img.filter(ImageFilter.BLUR)
- Sharpen Image
- Finding Edges
edges = sharped.filter(ImageFilter.FIND_EDGES)
- Enhancing Edges
en_edges = sharped.filter(ImageFilter.EDGE_ENHANCE)
- Emboss Filter
emb = sharped.filter(ImageFilter.EMBOSS)
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.
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.