Search code examples
pythongrayscalescikit-imageimread

Image does not load as grayscale (skimage)


I'm trying to load an image as grayscale as follows:

from skimage import data
from skimage.viewer import ImageViewer

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

However, if I check for its shape using img.shape it turns out to be a three-dimensional, and not two-dimensional, array. What am I doing wrong?


Solution

  • From scikit-image documentation, the signature of data.imread is as follows:

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

    Your code does not work properly because the keyword argument as_grey is misspelled (you put as_gray).

    Sample run

    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)