Python Pillow module – All you need to know!

Filed Under: Python Modules
Python Pillow Module

Hey, readers! In this article, we will be focusing on Python Pillow module, in detail. So, let us begin! 馃檪


Use of Python Pillow module

Before diving deep into the concepts of Pillow module, let me give you a premise of this concept.

These days image processing and modeling has gained a lot of attention because of its implementation in real life scenarios. With image processing gaining popularity, Python came up with PIL module that enables us to process and make manipulations to the image. This gave us a base to the image processing concepts.

But, with the Python3 version, PIL was no more supported. That is when Pillow module came into the picture. That is, the Pillow module replaced the PIL module in Python.

With more upgradation on the features, Python Pillow module offers way more than just processing of images.

In the upcoming module, will be exploring some of the mostly used features of Pillow module.


Features offered by Pillow module

Having understood that the module works for image processing, it is very important to understand the process to load the image. Pillow makes use of the internal Image submodule to load the image.

Syntax–

Image.open(image file)

With open() function, we can import and load the base image into the Python environment for manipulation. The show() function enables us to view the image in the local environment.

We have made use of the below simple screenshot of CMD for learning purpose as shown below–

Demo Image
Demo Image

1. Rotation of image

With the Pillow module, we can rotate the image to any specific angle using rotate() function. The rotate() function enables us to perform the action of rotation of images.

Example-

In this example, we have opened a demo image and then rotated the image to an angle of 42 degrees.

from PIL import Image
img = Image.open("C:/Users/HP/OneDrive/Desktop/demo.png")
img_rotate = img.rotate(42)
img_rotate.show()

Output–

Rotated Image
Rotated Image

2. Blurring an Image

Apart from rotation, the Pillow module also offers us the below methods that can be used to blur an image to any customized extent.

  1. Gaussian Blur – This gives the smoothest blur effect to the image. Even the edges and corners of the images in the minute differences are eliminated.
  2. Box Blur – Here, we specify the radius that is the intensity of the blur effect to happen on any image. Post which we can view a customized blur effect.
  3. Simple Blur – With Simple blur, a basic blur effect without any customization is observed.

In the below example, we have implemented the Box Blur effect and provided a radius of 4 for the effect to take place.

Example–

from PIL import ImageFilter
from PIL import Image
img = Image.open("C:/Users/HP/OneDrive/Desktop/demo.png")
img.convert('RGB')
blure_img = img.filter(ImageFilter.BoxBlur(4))
blure_img.show()

Output:

Blur Image
Blur Image

3. Watermark an Image

Apart from image customizations, we can even have watermarks on the image in a user defined manner. The ImageDraw subprocess enables us to add a user-defined Watermark to the image.

Initially, we open the image with open() function. Post which, we use draw() function to have the image for edition.

We can set the size of the watermark and the value in the text() function which will be implemented in the base image itself.

Example–

from PIL import ImageDraw, ImageFont
from PIL import Image
img = Image.open("C:/Users/HP/OneDrive/Desktop/demo.png")
 
draw_img = ImageDraw.Draw(img)

draw_img.text((200, 150),"Demo Mark")
img.show()

Output-

Watermark
Watermark

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to Python programming, Stay tuned with us.

Till then, Happy Learning!! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content