Easily Manipulate pillow Image
Image Processing With the Python Pillow Library
When you look at an image, you see the objects and people in it. However, when you read an image programmatically with Python or any other language, the computer sees an array of numbers. In this tutorial, you’ll learn how to manipulate images and perform basic image processing using the Python Pillow library.
Pillow and its predecessor, PIL, are the original Python libraries for dealing with images. Even though there are other Python libraries for image processing, Pillow remains an important tool for understanding and dealing with images.
To manipulate and process images, Pillow provides tools that are similar to ones found in image processing software such as Photoshop. Some of the more modern Python image processing libraries are built on top of Pillow and often provide more advanced functionality.
In this tutorial, you’ll learn how to:
- Read images with Pillow
- Perform basic image manipulation operations
- Use Pillow for image processing
- Use NumPy with Pillow for further processing
- Create animations using Pillow
This tutorial provides an overview of what you can achieve with the Python Pillow library through some of its most common methods. Once you gain confidence using these methods, then you can use Pillow’s documentation to explore the rest of the methods in the library. If you’ve never worked with images in Python before, this is a great opportunity to jump right in!
In this tutorial, you’ll use several images, which you can download from the tutorial’s image repository:
With those images in hand, you’re now ready to get started with Pillow.
Basic Image Operations With the Python Pillow Library
The Python Pillow library is a fork of an older library called PIL. PIL stands for Python Imaging Library, and it’s the original Python library for image processing. Pillow was created as a modern alternative to PIL, with added features and improvements.
Pillow provides the Image module, which is responsible for reading images in various formats and representing them as Image objects. The Image class provides methods for manipulating and saving images.
To begin, you’ll need to install Pillow. Open a terminal or command prompt and run the following command:
With Pillow installed, you can start working with images in Python. Open a new Python script or interactive shell, and import the Image module:
The Image Module and Image Class in Pillow
The Image module is the main entry point for working with images in Pillow. It contains the Image class, which represents an image and provides methods for manipulating it.
To open an image file with Pillow, you can use the Image.open()
method. Provide the path to the image file as an argument:
This will create an Image object named image
from the specified image file. You can now perform various operations on this image.
Basic Image Manipulation
Pillow provides several basic image manipulation methods that you can use to modify images. Here are a few examples:
-
Resize: You can resize an image to a specific width and height using the
resize()
method: -
Crop: You can crop an image to a specific rectangular region using the
crop()
method: -
Rotate: You can rotate an image by a specified angle using the
rotate()
method:
These are just a few examples of the basic image manipulation methods available in Pillow. You can explore the documentation to learn more about the different methods and their parameters.
Bands and Modes of an Image in the Python Pillow Library
In Pillow, images are represented as a collection of bands, where each band represents a different channel of information. For example, a colored image may have separate bands for the red, green, and blue channels.
The number and types of bands in an image depend on its mode. The mode of an image defines how pixel values are represented. Here are some examples of common image modes:
- L: Luminance (grayscale)
- RGB: Red, green, blue
- RGBA: Red, green, blue, alpha (transparency)
You can check the mode of an image using the mode
attribute:
You can also split the bands of an image into separate Image objects using the split()
method:
This will return a tuple containing separate Image objects for each band.
Understanding bands and modes is important when performing advanced image processing tasks, such as applying filters or manipulating individual channels.
Image Processing Using Pillow in Python
Now that you know the basics of working with images in Pillow, let’s dive into some more advanced image processing techniques.
Image Filters Using Convolution Kernels
Image filters are operations that manipulate pixels in an image to achieve various effects. Pillow provides several built-in filters that you can apply to an image.
To apply a filter, you’ll need to use a convolution kernel. A convolution kernel is a matrix of numbers that specifies how pixels are combined to compute a new value. Different convolution kernels produce different effects.
Here’s an example of applying a blur filter to an image using the GaussianBlur filter:
You can experiment with different filters and parameters to achieve different effects.
Image Blurring, Sharpening, and Smoothing
Blurring, sharpening, and smoothing are common image processing techniques used to enhance or alter an image.
Pillow provides methods for blurring, sharpening, and smoothing an image. Here are some examples:
-
Blur: You can blur an image using the
filter()
method with theBLUR
orGAUSSIAN_BLUR
filter: -
Sharpen: You can sharpen an image using the
filter()
method with theSHARPEN
filter: -
Smooth: You can smooth an image using the
filter()
method with theSMOOTH
filter:
Experiment with different values and combinations of these filters to achieve the desired effect.
Edge Detection, Edge Enhancement, and Embossing
Edge detection, edge enhancement, and embossing are techniques used to highlight or emphasize the edges of objects in an image.
Pillow provides methods for performing these operations. Here are a few examples:
-
Edge Detection: You can perform edge detection using the
filter()
method with theFIND_EDGES
filter: -
Edge Enhancement: You can enhance the edges of an image using the
filter()
method with theEDGE_ENHANCE
filter: -
Embossing: You can create an embossed effect using the
filter()
method with theEMBOSS
filter:
Play around with these techniques to see how they can bring out the details and edges of objects in an image.
Image Segmentation and Superimposition: An Example
Image segmentation is the process of dividing an image into multiple segments or regions. Superimposition is the process of combining multiple images into one.
In this section, you’ll learn how to perform image segmentation and superimposition using Pillow.
Image Thresholding
Image thresholding is a technique used to separate objects from the background in an image. It works by converting an image to black and white, where pixels with values above a certain threshold are set to white, and pixels below the threshold are set to black.
Here’s an example of performing image thresholding using Pillow:
This will convert the image to grayscale ('L'
mode), and then apply a custom thresholding function to convert the pixels to either black or white.
Erosion and Dilation
Erosion and dilation are morphological operations used to modify the shape and size of objects in an image.
Pillow provides methods for erosion and dilation. Here’s an example:
This will erode the image by a specified size, shrinking the objects and removing small details.
Image Segmentation Using Thresholding
Now that you have learned about image thresholding, erosion, and dilation, you can combine these techniques to perform image segmentation.
This will convert the image to grayscale, apply a threshold, and then perform erosion to segment the objects in the image.
Superimposition of Images Using Image.paste()
Superimposing images involves combining multiple images into one by placing one image onto another. Pillow provides the paste()
method, which allows you to overlay one image onto another.
Here’s an example of superimposing two images using Pillow:
This will open two images, background
and overlay
, and superimpose overlay
onto background
at the specified position (x, y)
. The optional mask
parameter is used to specify a transparency mask for the overlay.
Creation of A Watermark
Watermarking is the process of adding a visible mark or logo to an image to identify its source or protect its copyright.
Pillow provides methods for creating watermarks. Here’s an example:
This will open a watermark image and create a copy of the background image. It then pastes the watermark onto the copy at the specified position (x, y)
.
Image Manipulation With NumPy and Pillow
NumPy is a powerful Python library for numerical computing. When combined with Pillow, you can perform advanced image manipulation and processing tasks.
Using NumPy to Subtract Images From Each Other
NumPy provides functions for performing mathematical operations on arrays, including images. Here’s an example of using NumPy to subtract one image from another:
This will convert the images to NumPy arrays, subtract one from the other, and then take the absolute value of the result to ensure positive values.
Using NumPy to Create Images
NumPy arrays can be used to create new images from scratch. Here’s an example:
This will create a black image with the specified width and height, and then draw a blue rectangle on it.
Creating Animations
By combining NumPy and Pillow, you can create animations from a sequence of images. Here’s an example:
This will load a sequence of images, convert them to NumPy arrays, and then save them as an animated GIF.
Conclusion
In this tutorial, you’ve learned how to manipulate and process images using the Python Pillow library. You’ve seen how to perform basic image operations, apply filters, perform image segmentation and superimposition, and manipulate images using NumPy. With these techniques, you can explore and expand your knowledge of image processing in Python.
Remember that Pillow provides many more methods and functionality than what was covered in this tutorial. Make sure to refer to Pillow’s documentation for a comprehensive guide to all the available features and methods.
Now that you have a good understanding of how to work with images in Python, you can use Pillow to explore and experiment with different image processing tasks and techniques.