views:

51

answers:

2

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? I suspect it's something stupid.

Thanks for the help!

+2  A: 

Try to use a grayscale colormap?

E.g. something like

imshow(..., cmap=pyplot.cm.binary)

For a list of colormaps, see http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps

janneb
+1  A: 
import numpy as np
import pylab
import matplotlib.cm as cm
import Image

fname='cartoon.png'
image=Image.open(fname).convert("L")
arr=np.asarray(image)
pylab.imshow(arr,cmap=cm.Greys_r)
pylab.show()
unutbu