Cannot reshape array using imageio. Why?

Bn.F76 picture Bn.F76 · Dec 14, 2017 · Viewed 12.5k times · Source

I'm trying to mimic what this line of code does, using imageio :

img_array = scipy.misc.imread('/Users/user/Desktop/IMG_5.png', flatten=True)
img_data  = 255.0 - img_array.reshape(784)`

However when using imageio I get:

img = imageio.imread('/Users/user/Desktop/IMG_5.png')
img.flatten()

Output: Image([212, 211, 209, ..., 192, 190, 191], dtype=uint8)

img.reshape(1, 784)
ValueError: cannot reshape array of size 2352 into shape (1,784)

Can someone explain what is going on here, why is my image size 2352? I resized the image to 28x28 pixels before importing it.

Answer

Rodrigo Laguna picture Rodrigo Laguna · Jul 4, 2018

I know this question already has an accepted answer, however, it implies to use skimage library instead of imageio as the question (and scipy) suggest. So here it goes.

According to imageio's doc on translating from scipy, you should change flatten argument by as_gray argument.

So this line:

img_array = scipy.misc.imread('/Users/user/Desktop/IMG_5.png', flatten=True)

should gives you same result as this:

img_array = imageio.imread('/Users/user/Desktop/IMG_5.png', as_gray=True)

It worked for me. If it didn't work for you, perhaps there is another problem. Providing an image as an example might help.