Python Program for Negative Transformation of an Image Using PIL

In this post, we will see how to use PIL in Python to perform a negative transformation on an image.

PIL Module:

PIL is an abbreviation for Python Imaging Library. It is used in Python to do some basic actions on images. As a result, we must use Image Class from the PIL for this task.

Installation:

pip install PIL

In this scenario, the Image Class is made up of numerous properties and methods. We need open, show, getpixel, and putpixel methods to open an image, see an image, retrieve pixels from a given picture, and change pixels in an image.

Python code to Open an Image

# Import Image function from PIL module using the import keyword
from PIL import Image
# Import pyplot of the matplotlib library using the import keyword
import matplotlib.pyplot as plt
# Open the image by passing the image path as an argument to the open() function
# and store it in a variable.
gvn_img= Image.open("demo_image.jpg")
# Make axis = 'off' to remove the x and y axis from the plot 
plt.axis('off')
# Display the above given image using the imshow() function
plt.imshow(gvn_img)

Output:

<matplotlib.image.AxesImage at 0x7f342c9c0c90>

Program for Negative Transformation of an Image Using PIL in Python

As we all know, in the RGB colour model, each colour has a decimal index ranging from 0-255. The value 0 represents the lowest and the value 255 represents the highest. For example, (255,255,255) denotes white. Similarly, (0,0,0) represents black. We are meant to conduct the Negative Transformation of the Color, which means inverting the colour as seen below.

Allow Color X to be represented as (r,g,b)= (100,100,100). It can be changed as follows:

R=255 – r =255 – 100 = 155 => R=155

Likewise, G=155 and B=155

As a result, the Negatively Transformed Color Indices of X are (R,G,B) = (155,155,155)

So, let’s apply Negative Color Transformation to each pixel of an image.

Example

Approach:

  • Import Image function from PIL module using the import keyword.
  • Import pyplot of the matplotlib library using the import keyword.
  • Open the image by passing the image path as an argument to the open() function and store it in a variable.
  • Get the width and height of the image using size attribute.
  • Loop till the width of the image using the for loop.
  • Loop again till the height of the image using another nested for loop.
  • Get the r, g, b pixels of the image using getpixel() function.
  • Subtract 255 from the given r to get its exact value.
  • Similarly, do the same for g and b.
  • Pass the modified r, g, b values to the putpixel() function(converting into negative image).
  • Make axis = ‘off’ to remove the x and y axis from the plot.
  • Display the above given image using the imshow() function

Below is the implementation:

# Import Image function from PIL module using the import keyword
from PIL import Image
# Import pyplot of the matplotlib library using the import keyword
import matplotlib.pyplot as plt
# Open the image by passing the image path as an argument to the open() function
# and store it in a variable.
gvn_img= Image.open("demo_image.jpg")
# Get the width and height of the image using size attribute
width,height = gvn_img.size
# Loop till the width of the image using the for loop
for m in range(width):
    # Loop again till the height of the image using another nested for loop
    for n in range(height):
        # Get the r, g, b pixels of the image using getpixel() function
        r,g,b=gvn_img.getpixel((m,n))
        # Subtract 255 from the given r to get its exact value. 
        # Similarly do the same for g and b
        r=255-r
        g=255-g
        b=255-b
        # Pass the modified r, g, b values to the putpixel() function(converting into negative image)
        gvn_img.putpixel((m,n),(r,g,b))

# Make axis = 'off' to remove the x and y axis from the plot 
plt.axis('off')
# Display the above given image using the imshow() function
plt.imshow(gvn_img)

Output:

<matplotlib.image.AxesImage at 0x7f342b3e2b10>