Plot negative of an image in Python

In this session, we are going to learn how we can convert a given image into its negative form.

How to convert an image into its negative image in Python

Negatives image means brighter pixels becomes darker and darker becomes brighter.so for we have to use the following formula:

pixels value = 255-r
              where r=input images pixels value
                  255=maximum value of color range

Now first write code for display the given image:

#important library to show the image 
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
#importing numpy to work with large set of data.
import numpy as np
#image read function
img=mpimg.imread('images.jpg')
#image sclicing into 2D. 
x=img[:,:,0]
# x co-ordinate denotation. 
plt.xlabel("Value")
# y co-ordinate denotation.
plt.ylabel("pixels Frequency")
# title of an image .
plt.title("Original Image")
# imshow function with comperision of gray level value.
plt.imshow(x,cmap="gray")
#plot the image on a plane.
plt.show()

Output image:

convert image to negative image in Python




convert its negative image:

y=np.shape(x)
z=np.zeros(y)
#convert the image into its negative value.
z=255-x
plt.xlabel("Value")
plt.ylabel("pixels Frequency")
plt.title("Negative image ")
plt.imshow(z,cmap="gray")
plt.show()

Now combine the whole program:

#important library to show the image 
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
#importing numpy to work with large set of data.
import numpy as np
#image read function
img=mpimg.imread('images.jpg')
#image sclicing into 2D. 
x=img[:,:,0]
# x co-ordinate denotation. 
plt.xlabel("Value")
# y co-ordinate denotation.
plt.ylabel("pixels Frequency")
# title of an image .
plt.title("Original Image")
# imshow function with comperision of gray level value.
plt.imshow(x,cmap="gray")
#plot the image on a plane.
plt.show()

y=np.shape(x)
z=np.zeros(y)
#convert the image into its negative value.
z=255-x
plt.xlabel("Value")
plt.ylabel("pixels Frequency")
plt.title("Negative image ")
plt.imshow(z,cmap="gray")
plt.show()

Output image :

convert image into negative in Python

Also read:


Leave a Reply

Your email address will not be published. Required fields are marked *