In this article, we are going to depict images using the Matplotlib module in grayscale representation using PIL, i.e. image representation using two colors only i.e. black and white.
Syntax: matplotlib.pyplot.imshow(X, cmap=None)
Displaying Grayscale image
Displaying Grayscale image, store the image path here let’s say it fname. Now open the image using PIL image method and convert it to L mode If you have an L mode image, that means it is a single-channel image – normally interpreted as grayscale. It only stores a grayscale, not color. Plotting the image as cmap = ‘gray’ converts the colors. All the work is done you can now see your image.
Python3
fname = r 'g4g.png'
image = Image. open (fname).convert( "L" )
plt.imshow(image, cmap = 'gray' )
plt.show()
|
Output:
Example 1:
Python3
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
fname = r 'gfg.png'
image = Image. open (fname).convert( "L" )
plt.imshow(image, cmap = 'gray' )
plt.show()
|
Output:

Image used
Example 2:
Python3
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
fname = r 'geeks.png'
image = Image. open (fname).convert( "L" )
plt.imshow(image, cmap = 'gray' )
plt.show()
|
Output:

Image Used
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!