I'm trying to display a grayscale image using matplotlib.pyplot.imshow(). My problem is that the grayscale image is displayed as a colormap. I need the grayscale because I want to draw on top of the image with color.
I read in the image and convert to grayscale using PIL's Image.open().convert("L")
image = Image.open(file).convert("L")
Then I convert the image to a matrix so that I can easily do some image processing using
matrix = scipy.misc.fromimage(image, 0)
However, when I do
figure()
matplotlib.pyplot.imshow(matrix)
show()
it displays the image using a colormap (i.e. it's not grayscale).
What am I doing wrong here?
Try to use a grayscale colormap?
E.g. something like
For a list of colormaps, see http://scipy-cookbook.readthedocs.org/items/Matplotlib_Show_colormaps.html
The following code will load an image from a file
image.png
and will display it as grayscale.If you want to display the inverse grayscale, switch the cmap to
cmap='gray_r'
.try this:
@unutbu's answer is quite close to the right answer.
By default, plt.imshow() will try to scale your (MxN) array data to 0.0~1.0. And then map to 0~255. For most natural taken images, this is fine, you won't see a different. But if you have narrow range of pixel value image, say the min pixel is 156 and the max pixel is 234. The gray image will looks totally wrong. The right way to show an image in gray is
Let's see an example:
this is the origianl image: original
this is using defaul norm setting,which is None: wrong pic
this is using NoNorm setting,which is NoNorm(): right pic
I would use the get_cmap method. Ex.:
import matplotlib.pyplot as plt
You can also run once in your code
This will show the images in grayscale as default