Open In App

How to Display an Image in Grayscale in Matplotlib?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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
# storing image path
fname = r'g4g.png'

# opening image using pil
image = Image.open(fname).convert("L")

# mapping image to gray scale
plt.imshow(image, cmap='gray')
plt.show()

Output:

Display an Image in Grayscale in Matplotlib
 

Example 1:

Python3
# importing libraries.
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image


# storing image path
fname = r'gfg.png'

# opening image using pil
image = Image.open(fname).convert("L")

# mapping image to gray scale
plt.imshow(image, cmap='gray')
plt.show()

Output:

Image used
Display an Image in Grayscale in Matplotlib
 

Example 2:

Python3
# importing libraries.
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image


# storing image path
fname = r'geeks.png'

# opening image using pil
image = Image.open(fname).convert("L")

# mapping image to gray scale
plt.imshow(image, cmap='gray')
plt.show()

Output:

Image Used
 

Article Tags :
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy