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 npimport matplotlib.pyplot as pltfrom PIL import Imagefname = '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'
.
Try to use a grayscale colormap?
E.g. something like
imshow(..., cmap=pyplot.cm.binary)
For a list of colormaps, see http://scipy-cookbook.readthedocs.org/items/Matplotlib_Show_colormaps.html
import matplotlib.pyplot as plt
You can also run once in your code
plt.gray()
This will show the images in grayscale as default
im = array(Image.open('I_am_batman.jpg').convert('L'))plt.imshow(im)plt.show()