Negative transformation using Python and Pillow

Overview:

  • Inverting a digital image is a point processing operation.
  • The output of image inversion is a negative of a digital image.
  • In a digital image the intensity levels vary from 0 to L-1. The negative transformation is given by s=L-1-r.
  • When an image is inverted, each of its pixel value ‘r’ is subtracted from the maximum pixel value L-1 and the original pixel is replaced with the result ‘s’.
  • Image inversion or Image negation helps finding the details from the darker regions of the image.
  • The negative transformation has its applications in the areas of Medical Imaging, Remote Sensing and others.

 

Example:

#----- Example Python program for negative transformation of a Digital Image -----

 

# import Pillow modules

from PIL import Image

from PIL import ImageFilter

 

# Load the image

img = Image.open("./fishcatch.jpg");

 

# Display the original image

img.show()

 

# Read pixels and apply negative transformation

for i in range(0, img.size[0]-1):

    for j in range(0, img.size[1]-1):

        # Get pixel value at (x,y) position of the image

        pixelColorVals = img.getpixel((i,j));

       

        # Invert color

        redPixel    = 255 - pixelColorVals[0]; # Negate red pixel

        greenPixel  = 255 - pixelColorVals[1]; # Negate green pixel

        bluePixel   = 255 - pixelColorVals[2]; # Negate blue pixel

                   

        # Modify the image with the inverted pixel values

        img.putpixel((i,j),(redPixel, greenPixel, bluePixel));

 

# Display the negative image

img.show();

 

Output:

Before applying negative transformation to a digital image using Pillow-the Python Image Processing Library

 

After applying negative transformation to a digital image using Pillow-the Python Image Processing Library


Copyright 2024 © pythontic.com