Image Processing is used to extract useful information from an Image or an Image dataset. In image processing, we try and study different aspects of images and scientifically extract and analyze the information provided by the image.
Pgmagick is an open-source python library and a wrapper for GraphicsMagick, which is a robust collection of tools and libraries to read, write, and manipulate an image. It supports around 88 formats of image.
In this article, we will explore Pgmagick and its image processing capabilities. We will see how helpful and easy it is to extract useful information from images.
According to the official page of pgmagick currently, there is no official support for pgmagick on the Windows platform but you can download an unofficial binary version for windows.
After downloading the binary version you need to install it using pip install <path>, the path is the location where you downloaded the binary version.
- Importing required libraries
We only need to import pgmagick for the image processing functions.
from pgmagick import Image as Imn
- Loading the Image
We need to select an image on which we will perform different image processing functions. I have an image of an animal that I am going to use for processing.
- Image Processing
Now we will perform some image processing functions on the image we have loaded, pgmagick provides a variety of functions. We will try to cover most of these functions.
We will start by detecting the size of the image using the size function.
The Sharpen function sharpens the image which takes the radius value of the
sharpness as a parameter. We will store every processed image as a new image. The output image is stored in the same directory where Python is running.
Blur function is used to blur the image. It requires the radius of blur and sigma of the object as the parameter.
- Edge Detection
Detecting the edges is one of the main functions provided by most of the image processing libraries. Pgmagick also has an edge detection function.
- Swirl Image
Swirl function swirls the image from the center according to the angle we provide as a parameter.
- Implode Image
Implode function implodes the pixel of the images around the center, we just need to pass the radius of the implode.
- Applying Gamma Function
Gamma function changes the level of the color format. Our image is in RGB format and we will change the color level.
- Solarized Image
Solarize function negates all the pixels and converts the images into a solarized image.
- Flip and Flop
Flip and flop functions are used to invert the images from top to bottom and left to right respectively.
The Emboss function replaces each pixel of the image to give a 3D effect.
Histogram equalization processes the contrast of the image and sets it according to image histogram. Equalize function is used for histogram equalization.
- Spread and Shear
The spread function is used to displace the pixel of the image according to a threshold value and shear function is used to slide the image along the x and the y-axis we just need to pass the degree of sliding for both x and y-axis.
- Creating Images
We can use pgmagick for image creation purposes also. It supports different types of image creation functions.
- Background Image
from pgmagick.api import Image as Imapi
img = Imapi((200, 200), 'aqua')
- Animated Gif Image
We can create animated gifs using pgmagick but it has certain limitations while working on the windows system. As we are using the binary file which is not official some of the functions are not properly working.
from pgmagick import Image, ImageList, Geometry, Color
imgs = ImageList()
for color in ('violet', 'black', 'aqua', 'magenta', 'yellow'):
imgs.append(Image(Geometry(400, 400), Color(color)))
The code above will create an animated gif with the name ‘animate.gif’ in your system.
In this article, we saw different functionalities that Pgmagick provides. We saw how we can render images and perform different operations on different images with just a single line of code. Pgmagick is not officially available for Windows but is available for macOS and Linux based systems. For windows, we can download the binary file and install it to use pgmagick.
If you loved this story, do join our Telegram Community.
Also, you can write for us and be one of the 500+ experts who have contributed stories at AIM. Share your nominations here.
An aspiring Data Scientist currently Pursuing MBA in Applied Data Science, with an Interest in the financial markets. I have experience in Data Analytics, Data Visualization, Machine Learning, Creating Dashboards and Writing articles related to Data Science.