Now Reading
Complete Guide On Pgmagick – Python Tool For Image Processing

Complete Guide On Pgmagick – Python Tool For Image Processing

Himanshu Sharma
pgmagick image processing
W3Schools

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.



Implementation:

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.

  1. Importing required libraries

We only need to import pgmagick for the image processing functions.

from pgmagick import Image as Imn

  1. 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.

img =Imapi('tiger.jpg')

  1. 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.

  • Size

We will start by detecting the size of the image using the size function.

print(img.width, img.height)

  • Sharpen

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.

img.sharpen(10)

img.write('sharped.jpg')

Sharped Image
  • Blur

Blur function is used to blur the image. It requires the radius of blur and sigma of the object as the parameter.

img.blur(15,5)

img.write('blurred.jpg')

Blurred Image
  • 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.

img.edge(2)

img.write('edges.jpg')

  • Swirl Image

Swirl function swirls the image from the center according to the angle we provide as a parameter. 

img.swirl(30)

img.write('swirlled.jpg')

  • Implode Image

Implode function implodes the pixel of the images around the center, we just need to pass the radius of the implode.

img.implode(1)

img.write("imploded.jpg")

Imploded Image
  • 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.

img.gamma(1,4,8)

img.write('gam.jpg')

Gamma Image
  • Solarized Image

Solarize function negates all the pixels and converts the images into a solarized image.

img.solarize(20)

img.write('solarized.jpg')

Solarized Image
  • Flip and Flop

Flip and flop functions are used to invert the images from top to bottom and left to right respectively.

img.flip()

img.write('flipped.jpg')

img.flop()

img.write('flopped.jpg')

Pgmagick
  • Emboss

The Emboss function replaces each pixel of the image to give a 3D effect. 

img.emboss(10)

img.write('embossed.jpg')

Pgmagick
  • Equalize

Histogram equalization processes the contrast of the image and sets it according to image histogram. Equalize function is used for histogram equalization.

See Also
transfer learning models

img.equalize()

img.write('equalized.jpg')

Pgmagick
  • 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.

img.spread(10)

img.write('spreaded.jpg')

Pgmagick

img.shear(23,56)

img.write('sheared.jpg')

Pgmagick
  1. 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')

img.write('back_aq1.jpg')

Pgmagick
  • 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)))

imgs.animationDelayImages(50)

imgs.scaleImages(Geometry(100, 100))

imgs.writeImages('animate.gif') 

The code above will create an animated gif with the name ‘animate.gif’ in your system.

Conclusion:

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.

What Do You Think?

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.

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top