Have you ever wanted to edit an image but didn’t know where to start? Don’t panic.
The Pillow module has got you covered. The Python Imaging Library (PIL) has been around for years.
However, its production stopped in 2011, and as of Python 3.x, it’s no longer the go-to image processing module for Python developers. Its successor, the Pillow module, offers more advanced functionalities that developers can use to perform sophisticated image processing tasks.
In this article, we will give you an overview of the Pillow module, highlighting its differences with PIL. We will then guide you on how to implement the module with a hands-on guide.
You’ll learn how to import the module and load an initial image. We’ll also teach you how to rotate images, create image thumbnails, and add watermarks.
Finally, we’ll show you how to blur images using three distinct techniques: simple blur, box blur, and Gaussian blur.
Overview of Pillow Module
The Pillow module is a fork of the Python Imaging Library (PIL), which is an obsolete Python module for opening, manipulating, and saving image files. Pillow supersedes PIL and is recommended to be used instead.
The module offers a wide range of image manipulation functionalities that make it easy to apply image transformations, filters, and effects. With the Pillow module, you can complete basic to complex image editing tasks, including resizing, cropping, flipping, rotating, and annotating images.
Differences between Pillow Module and PIL Module
Pillow and PIL are similar in design, but there are some differences between them. One of the significant functional distinctions is that Pillow consists of additional features that PIL does not have.
As mentioned earlier, the Pillow module is an upgraded version of PIL and should be used instead. The Pillow module supports Python 3.x, while PIL only works in Python 2.x. Pillow also has better support for file formats, improved performance, and additional filters than PIL.
Implementation of Pillow Module
Importing the Module and Loading Initial Image
The first thing we’ll need to do is import the Pillow module. To do this, we’ll use the command as follows:
from PIL import Image
Once we have successfully imported the module, we can then use it to load our initial image from the file system.
Here’s the code:
image = Image.open("example_image.jpg")
With these two commands, we’ve imported the Pillow module and opened the image we want to work on.
Rotating an Image
With the image loaded, we can begin applying transformations to it. One of the most common image transformations is rotation.
The Pillow module makes it easy to rotate images. Here’s the code to rotate an image:
image.rotate(45).show()
This code rotates the image by 45 degrees and displays it.
Notice that we’re using the .show()
function to display the transformed image.
Creating Thumbnails from Normal Images
If we’re working with large images, we might want to create image thumbnails to give users a preview of the image. Creating thumbnails is easy using the Pillow module.
Here is the code we can use:
image.thumbnail((100, 100))
image.show()
This code creates a 100×100 thumbnail of our image and displays it. Notice that we’re using the .thumbnail()
function to create the thumbnail.
Blurring an Image
Blurring an image can add emphasis to specific areas of an image, or it can be used to anonymize individuals or objects in images. The Pillow module offers three distinct techniques for blurring images: simple blur, box blur, and Gaussian Blur.
Simple Blur
The simple blur function is the most basic blur function in the Pillow module. Here’s the code:
from PIL import ImageFilter
image.filter(ImageFilter.BLUR).show()
This code applies a simple blur effect to our image and displays it.
Notice that we’re using the ImageFilter.BLUR
function to apply the blur effect.
Box Blur
The box blur function is more advanced than the simple blur function.
It applies a smooth blur effect to the image, which is often used in image processing and computer graphics. Here’s the code:
image.filter(ImageFilter.BoxBlur(radius=5)).show()
This code applies the box blur effect to our image with a radius of 5 pixels.
Notice that we’re using the ImageFilter.BoxBlur
function to apply the blur effect.
Gaussian Blur
The Gaussian blur function is the most sophisticated blur function in the Pillow module.
It blurs the image while preserving the edges. Here’s the code:
image.filter(ImageFilter.GaussianBlur(radius=5)).show()
This code applies the Gaussian blur effect to our image with a radius of 5 pixels.
Notice that we’re using the ImageFilter.GaussianBlur
function to apply the blur effect.
Adding a Watermark on Images
Adding a watermark is a popular way of branding images or protecting them from being copied without permission. Here’s the code to add text watermark to our image:
from PIL import ImageDraw, ImageFont
font = ImageFont.truetype("Arial.ttf", 36)
draw = ImageDraw.Draw(image)
draw.text((0, 0), "Watermark", font=font)
image.show()
This code adds a text watermark to our image.
The font, font size, and watermark text can be customized to suit your needs.
Recap of the Pillow Module
In conclusion, the Pillow module is a powerful image processing library that can be used to perform various image editing tasks. It has replaced the now-obsolete Python Imaging Library (PIL) and offers Python developers more advanced functionalities than its predecessor.
The Pillow module is easy to use, supports Python 3.x, and has better support for file formats than PIL. In this article, we’ve covered how to import the Pillow module, load an initial image, rotate images, create image thumbnails, add watermarks, and apply various blur techniques.
By following our hands-on guide, you can confidently perform all sorts of image processing tasks.
Importing the Module and Loading Initial Image:
Before editing images, developers need to start by importing the Pillow module.
Once the module is imported, the next step is loading an initial image from the file system. This can be done using the Image.open()
function.
Rotating an Image:
Image rotation is a common image transformation used to change the orientation of an image. The Pillow module makes it easy to rotate images to the desired orientation using the .rotate()
function.
Developers specify the degree of rotation they require in the argument of the method.
Creating Thumbnails from Normal Images:
Creating a thumbnail of an image is done to make it easier for users to preview it. The Pillow module makes this easy using the .thumbnail()
method.
Developers specify the size of the thumbnail they need in the argument of the method.
Blurring an Image:
Blurring an image can be used to add emphasis to specific areas in an image or to anonymize individuals or objects. The Pillow module provides three distinct techniques for blurring images: simple blur, box blur, and Gaussian blur.
Adding a Watermark on Images:
Adding watermarks to images is an essential element in branding or protecting them from unauthorized use. Developers can use the Pillow module to add text or image watermarks onto images.
They do this using the ImageDraw
function and ImageFont
.
Future Tutorials
In subsequent articles, we will delve into more advanced functionalities of the Pillow module. We will cover topics such as composite images, converting images to different formats, color manipulation, Pixel Access, and Image Enhancements.
Composite images involve layering images over each other to create composite images. We will show you how to superimpose images and adjust the transparency of an image.
Converting images to different formats is useful when working with different file formats. We will show you how to convert an image from one file format to another.
Color manipulation is an essential functionality of the Pillow module. We will show you how to change the contrast of an image, apply color filters, adjust the brightness, and apply other color effects.
Pixel Access is a powerful function of the Pillow module that allows developers to read and write pixels in an image. We will be covering how to access pixels in an image, manipulate them, and enhance images.
Finally, Image Enhancements will cover topics like sharpening an image, enhancing image contrast, and image filtering. We will show you how to use the Pillow module’s advanced functionalities to enhance images and make them more visually appealing.
Final Thoughts:
Image processing is an exciting and rewarding field of study. With the Pillow module, Python developers can perform various image editing tasks, from basic to advanced.
This article has covered some of the fundamental functionalities of the module, and we look forward to exploring more advanced functionalities of the module in the subsequent articles.
In conclusion, the Pillow module is a powerful image processing library that offers Python developers a wide range of functionalities to perform various image editing tasks.
The module is easy to use, supports Python 3.x, and has better support for file formats than Pillow’s predecessor, the Python Imaging Library (PIL). By following our guide on how to import the module, load an initial image, rotate images, create image thumbnails, add watermarks, and apply various blur techniques, developers can confidently perform all sorts of image processing tasks.
We also looked at some advanced functionalities of the Pillow module that we’ll cover in subsequent articles. As the field of image processing continues to evolve, the Pillow module’s importance becomes more evident, and learning how to use it is essential for anyone interested in image editing.