Showing an image with pylab.imshow() Showing an image with pylab.imshow() python python

Showing an image with pylab.imshow()


it's just what the image is saved as in dna = mahotas.imread('dna.jpeg') type(dna) gives numpy.ndarray and dna.shape gives (1024, 1344, 1)

This is the problem, if you pass a 3D ndarray, it expects that you will have 3 or 4 planes (RGB or RGBA) (Read the code on line 410 in the last frame of the stack trace).

You just need to get rid of the extra dimension using

dna = dna.squeeze()

or

imshow(dna.squeeze())

To see what squeeze is doing, see the following example:

a = np.arange(25).reshape(5, 5, 1)print a.shape # (5, 5, 1)b = a.squeeze()print b.shape # (5, 5)


As of v3.3 this error is no longer raised, as 3d arrays of size MxNx1 are now coerced into MxN for displaying.


import matplotlib.pyplot as pltplt.imshow(img.reshape(48, 48)) # for example: 48