Home > Article > Backend Development > How to Display Grayscale Images Correctly in Matplotlib?
Displaying Images as Grayscale
Many image manipulation tasks often require grayscale images for ease of processing. Displaying grayscale images using Matplotlib's imshow() function can be challenging when the image is accidentally rendered as a colormap.
To resolve this issue and display a grayscale image correctly, follow these steps:
<code class="python">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()</code>
Alternatively, to display the inverse grayscale, simply change the cmap parameter to 'gray_r'.
The above is the detailed content of How to Display Grayscale Images Correctly in Matplotlib?. For more information, please follow other related articles on the PHP Chinese website!