Search code examples
pythonpython-3.xscikit-image

Reshaping png image read by skimage.imread return ValueError


It works fine using a 616x346 png image as input in the following code:

from skimage import io

image = io.imread('img.png')
image = image.reshape(image.shape[0] * image.shape[1], 3)

...but if I change the image dimensions to say 640x451, I get the error

ValueError: cannot reshape array of size 1154560 into shape (288640,3)

Any thouhts?


Solution

  • The shape of 640x451 image you are trying to reshape is (640, 451, 4) instead of (640, 451, 3). That's why you won't be able to convert it to (640*451, 3). Have a look at the output of image.shape in both the 616x346 and 640x451 cases. One workaround is to convert it to rgb from rgba first -

    from skimage import io, color
    image = io.imread('img.png')
    image = color.rgba2rgb(image)
    image = image.reshape(image.shape[0] * image.shape[1], 3)