Image does not load as grayscale (skimage)

I am trying to load a grayscale image like this:

from skimage import data
from skimage.viewer import ImageViewer

img = data.imread('my_image.png', as_gray=True)

      

However, if I check its shape with img.shape

, it turns out to be a 3D array, not a 2D array. What am I doing wrong?

+3


source to share


1 answer


From the scikit-image documentation, the signature data.imread

looks like this:

skimage.data.imread(fname, as_grey=False, plugin=None, flatten=None, **plugin_args)

      

Your code is not working correctly because the keyword argument as_grey

has a spelling mistake (you put as_gray

).



Run example

In [4]: from skimage import data

In [5]: img_3d = data.imread('my_image.png', as_grey=False)

In [6]: img_3d.dtype
Out[6]: dtype('uint8')

In [7]: img_3d.shape
Out[7]: (256L, 640L, 3L)

In [8]: img_2d = data.imread('my_image.png', as_grey=True)

In [9]: img_2d.dtype
Out[9]: dtype('float64')

In [10]: img_2d.shape
Out[10]: (256L, 640L)

      

+2


source







All Articles