Negative Transformation of an Image in Python

FeaImg Python Negative Transformation Of An Image

In this article, you will learn how to use PIL in Python to perform a negative transformation on an image. PIL is an abbreviation for Python Imaging Library. This library is used for multiple image transformations. PIL makes it super easy to work with images and can help you edit different parts of it without many lines of code.


Importing Modules

Let’s use the Image Class from the PIL for this purpose. As a result, the first line of the script will be as follows:

from PIL import Image

Image Class is made up of many properties and methods in this case. To open an image, see an image, retrieve pixels from a given image, and change pixels in an image, we require open, show, getpixel, and putpixel methods.

So, let’s have a look at how to open an Image. It is possible to do so as follows.

from PIL import Image
import matplotlib.pyplot as plt
img=Image.open("fig.jpg")
plt.axis('off')
plt.imshow(img)

Loading The Image

Here, img is the Image Object for the supplied image, which can be found at a path of the Image.” Let’s look at the Negative Transformation of Colors in an Image.

Negative Transformation Input
Negative Transformation Input

As we all know, under the RGB color model, each color 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 implies inverting the color as seen below.

Let Colour X is represented as (r,g,b)=(100,100,100). It can be transformed as follows

R=255-r =255-100=155         =>   R=155
Similarly, G=155 and B=155
Hence Negatively Transformed Colour Indices of X are (R,G,B) =(155,155,155)

Implementing Negative Transformation of an Image

So, let’s implement a Negative Transformation of Colours for each Pixel of an Image.

w,h=img.size
for i in range(w):
    for j in range(h):
        r,g,b=img.getpixel((i,j))
        r=255-r
        g=255-g
        b=255-b
        img.putpixel((i,j),(r,g,b))
plt.axis('off')
plt.imshow(img) 
Negative Transformation Output
Negative Transformation Output

Code for Negative Transformation of Images in Python

from PIL import Image
import matplotlib.pyplot as plt
img=Image.open("fig.jpg")

w,h=img.size
for i in range(w):
    for j in range(h):
        r,g,b=img.getpixel((i,j))
        r=255-r
        g=255-g
        b=255-b
        img.putpixel((i,j),(r,g,b))
plt.axis('off')
plt.imshow(img) 
Negative Transformation Input 2
Negative Transformation Input 2
Negative Transformation Output 2
Negative Transformation Output 2

Conclusion

Congratulations! You just learned how to get the Negative Transformation of an Image. Hope you enjoyed it! 😇

Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:

  1. Python Patchify – Extracting Patches from Large Images
  2. Classifying Clothing Images in Python – A complete guide
  3. Denoising Images in Python – A Step-By-Step Guide
  4. Visualizing Colors In Images Using Histograms – Python OpenCV

Thank you for taking your time out! Hope you learned something new!! 😄