Python Pillow Tutorial

A very popular and modern language, Python is used in almost every field of science. The reason behind the popularity of Python is that they keep providing extensive libraries and modules to their community that help programmers to perform difficult tasks using single-line modules. Python is used in artificial intelligence and robotics to perform operations using image processing techniques. Image processing like merging, cropping, filtering, segmentation, etc., is performed on the image with the help of Python libraries like Python Pillow.

This post provides an extensive tutorial on Python Pillow with the below-mentioned outcomes:

So let’s start!

What is Python Pillow in Python?

Python Pillow” is used in Python to do various image processing tasks on digital images. The digital images have the format JPEG, PNG, BMP, etc. Python pillow supports all file formats, and even the user can create a new decoder file to support any format. Python Pillow can be used to filter images, create thumbnails, crop images, etc.

Image Processing in Python Using Pillow

How to install Kali Linux in Window...

Processing of images helps us analyze and manipulate digital images. Different algorithms on images are performed to extract the information and use that information for further operations. Image processing techniques are used in traffic police cameras which identify information like vehicle number plates. Python pillow modules have various functions to perform operations on digital images. Some of the widely used Pillow modules are listed below:

  • Image module
  • ImageDraw
  • ImageFont
  • ImageGrab
  • ImageFilter
  • ImageColor

In the upcoming part of this guide, some pillow modules are explained with the help of examples.

How to Install Pillow in Python?

Python does not include a pillow module by default. It is an open-source module. To install the Python pillow into the system, we used the pip package installer in our terminal application. The below-listed command is used to install this module.

> pip install pillow

The above snippet shows the installation of the pillow package.

Working With Pillow in Python

Python Pillow package is used for various tasks to perform operations on digital images. We performed different manipulation on images using multiple functions of the pillow module. Some of the examples are given below.

Example 1: Opening the Image in Python Using Pillow Module

In the following example code, the Pillow module function named “image.open()” will be used to open an image. 

Code:

from PIL import Image

# Location of the image
img = Image.open("itslinuxfoss.png")

img.show()

In the above code:

  • The image “open()” and “show()” functions are imported from the Pillow module.
  • The location of the image is provided inside the parameter value of the “open()” function.
  • The“show()” function displays the image.

Output:

It is verified from the above snippet that the input image is displayed on the console screen.

Example 2: Getting the Size and Format of the Image

In the following example code, the Pillow module functions named “image.size()” and “image.format()” will be used to get the size and format of an input image.

Code:

from PIL import Image

img = Image.open("itslinuxfoss.png")

# To get the size
print(img.size)

# To get the format of the image
print(img.format)

In the above code:

  • The image module is imported from Pillow.
  • The size and format attributes are used to get the value of the size and format of the image. The size attribute returns its value in the form of the tuple that contains the width of the image and height.

Output:

The output shows the size of the image “202×68” and the image format “PNG”.

Example 3: Cropping the Image Using Pillow Module

In the following example code, the Pillow module function named “image.crop()” will crop the given input image. The “image.size()” function is used to find the size of the image.

Code:

from PIL import Image

img = Image.open(r"itslinuxfoss.png")

width, height = img.size

# defining the points for cropped image
left = 5
top = height / 4
right = 164
bottom = 3 * height / 4

img1 = img.crop((left, top, right, bottom))
img1.show()

In the above code:

  • The image module is imported from the Pillow library.
  • The image is open in “RGB” mode if we use “r” before the quotation marks.
  • The “size” attribute of the image is used to get the size.
  • The cropped image points are initialized from all sides.
  • crop()” function is used to crop the image according to the points.

Output:

The output shows the cropped image.

That was all from this extensive Python Pillow tutorial.

Conclusion

In Python, the pillow module is used for multiple operations on digital images like cropping, filtering, drawing and enhancing, etc. Image processing methods are applied to different images using the pillow module. The size, format, or any attributes of the image is easily extracted with the pillow module. This write-up has briefly explained the Python Pillow with multiple examples.