How to negatively transform an image in Python

Digital Image Processing is a significant aspect of data science. It is used in image modification and enhancement so that image attributes can be acquired to lead to a greater understanding of data.

An image is made up of elements called pixels. They are arranged in a two-dimensional manner, and are represented using squares.

There are three main types of images:

  1. RGB: Each pixel contains three values for the red, green, and blue color and is stored in three bytes. Each value is in the range 02550-255. The values combined make up the resultant color of the pixel.
  2. Greyscale: Values range from 02550-255 and represent the pixel intensity. Each pixel is stored in 88 bits. 00 depicts a white pixel, while 255255 depicts a black pixel.
  3. Binary: Each pixel is stored in one bit, and can have 00 or 255255 as its value. 00 depicts a white pixel, while 255255 depicts a black pixel.

Negative transformation

Negative transformation refers to subtracting pixel values from (L1)(L-1), where L is the maximum possible value of the pixel, and replacing it with the result.

To negatively transform an image, we loop through the pixels using two for loops. If the image is RGB, the red, green, and blue values are subtracted from (L1)(L-1) and the result is stored in place of the values. In the case of greyscale images, the intensity of the pixels is subtracted instead.

Negative transformation is done to bring attention to detail in the darker regions of the image.

Code

The code below performs the negative transformation process exactly as described earlier:

import PIL
#negative transformation function
def neg_trans(img):
#get width and height of image
width,height=img.size
#traverse through pixels
for x in range(width):
for y in range(height):
pixel_color=img.getpixel((x,y))
#if image is RGB, subtract individual RGB values
if type(pixel_color) == tuple:
#s=(L-1)-r
red_pixel=256-1-pixel_color[0]
green_pixel=256-1-pixel_color[1]
blue_pixel=256-1-pixel_color[2]
#replace the pixel
img.putpixel((x,y),(red_pixel,green_pixel,blue_pixel))
#if image is greyscale, subtract pixel intensity
else:
#s=(L-1)-r
pixel_color=256-1-pixel_color
#replace the pixel
img.putpixel((x,y),pixel_color)
return img

Explanation

The neg_trans function in the code above takes in an image as a parameter and traverses through its pixels.

The PIL library provides the function getpixel(), which returns the colour of the pixel present at a point, and putpixel(), which plots a pixel of specified color at a point.

The colour of the pixels are stored in line 1313 and checked to see if they have the data type tuple or int.

  • tuple means the pixel has three color channels, and the image is RGB.
  • int means the pixel has only one color channel, and the image is grayscale.

If the image is RGB, the image is transformed by subtracting the respective color intensities of each pixel from 255255, while for grayscale images, the singular intensity is subtracted from 255255 to obtain the negatively transformed image.

The putpixel() function is then used to plot the negatively transformed pixels in place of the original pixels, as you can see in lines 2424 and 3333.

Free Resources