Reputation: 12520
I'm following this tutorial here where I copy pasted the first code sample:
from scipy import misc
l = misc.lena()
misc.imsave('lena.png', l)
import matplotlib.pyplot as plt
plt.imshow(l)
plt.show()
The expected output according to the site is:
But this is what I'm getting:
As you can see there's an issue with the colours. First time programming with images so I have no clue what's happening here.
Upvotes: 1
Views: 219
Reputation: 27575
Matplotlib has a lot of included colormaps. So, when you plot using imshow
you can choose how to map shades of gray (or intensity values on your array) to a gradient of colors.
The default colormap is 'jet', but you could, for example, use the gray
colormap like this:
import matplotlib.pyplot as plt
from matplotlib import cm
plt.imshow(l, cmap=cm.gray)
plt.show()
Or use 'heat', or 'bone', or 'pink', to get different colored images.
Upvotes: 0
Reputation: 159
You need to change the color map that is used to display the image. If you change
plt.imshow(l)
to
plt.gray()
plt.imshow(l)
It should show you the correct result. Note that in this case, the colormap is set to gray levels on a global level. If you only want to change the colormap for a single call, assign the proper colormap to the "cmap" attribute of the "imshow(l)" call.
Upvotes: 0
Reputation: 21831
Your problem is that matplotlib by default uses a colormap that is not very suitable to show grayscale images.
It is easily solved by using the gray
colormap:
plt.gray() # Switch to grayscale
plt.imshow(l)
plt.show()
Something else to be aware of is that by default, the image is interpolated. This means that if you zoom in, you will not see the blocky pixels you expect, but a "blurry" image.
You can stop this by using the interpolation=
option:
plt.imshow(image, interpolation='none')
Upvotes: 2