Display Image as Grayscale Using Matplotlib

Display image as grayscale using matplotlib

The following code will load an image from a file image.png and will display it as grayscale.

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

fname = 'image.png'
image = Image.open(fname).convert("L")
arr = np.asarray(image)
plt.imshow(arr, cmap='gray', vmin=0, vmax=255)
plt.show()

If you want to display the inverse grayscale, switch the cmap to cmap='gray_r'.

How can I convert an RGB image into grayscale in Python?

How about doing it with Pillow:

from PIL import Image
img = Image.open('image.png').convert('L')
img.save('greyscale.png')

If an alpha (transparency) channel is present in the input image and should be preserved, use mode LA:

img = Image.open('image.png').convert('LA')

Using matplotlib and the formula

Y' = 0.2989 R + 0.5870 G + 0.1140 B 

you could do:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])

img = mpimg.imread('image.png')
gray = rgb2gray(img)
plt.imshow(gray, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
plt.show()

How to make grayscale image using numpy and matplotlib

Check out the answer here. The accepted answer shows doing this with both Pillow and numpy/matplotlib.

If the end goal is just to save the image out as a grayscale version then Pillow will do the job. If the goal is to send the grayscale version to some other part of the script where numpy/matplotlib is required you can either use the second part of the answer at the above link or convert the Pillow object to a numpy array as shown here.

opencv convert image to grayscale, and display using matplotlib gives strange color

It's because of the way Matplotlib maps single-channel output. It defaults to the "perceptially uniform" colourmap: blue->yellow; a bit like how you might expect a heatmap to be from blue to red, but theoretically clearer for human vision.

There's some more details here that might help:
https://matplotlib.org/users/colormaps.html

You need to tell it to use the gray colourmap when you show the image:

plt.imshow(arr, cmap='gray')

Also see this question: Display image as grayscale using matplotlib

Edit: Also, see lightalchemist's answer regarding mixing up RGB and BGR!

plt.gray() is not working and displaying original color image in matplotlib

Your image is RGB color. From the imshow docstring:

cmap : str or `~matplotlib.colors.Colormap`, default: :rc:`image.cmap`
The Colormap instance or registered colormap name used to map
scalar data to colors. This parameter is ignored for RGB(A) data.

Note in particular "This parameter is ignored for RGB(A) data."

If you want to display the image as grayscale, you'll have to "flatten" the colors somehow. There are several ways to do this; one is to display just one of the color channels, e.g.

plt.imshow(img[:, :, 0], cmap="gray")  # Display the red channel

Another popular method is to take a weighted average of the channels, with weights [0.299, 0.587, 0.113]:

imshow(img @ [0.299, 0.587, 0.113], cmap='gray')

See "Converting colour to grayscale" for more ideas.

Show grayscale OpenCV image with matplotlib

You are supposed to add another parameter in plt.imshow() so as to mention that you want a gray scale image.

Modify the last line like this: plt.imshow(img, cmap='gray')

Upon doing so I got the following:

Sample Image



Related Topics



Leave a reply



Submit