2

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.

Bn.F76
  • 783
  • 2
  • 12
  • 30

2 Answers2

4

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.

Rodrigo Laguna
  • 1,796
  • 1
  • 26
  • 46
2

An RGB image has three channels, so 784 pixels three times is 2352. Shouldn't you save the results of img.flatten() in a variable? img_flat = img.flatten(). If you do this you should get the three color layers flatten to one gray-scale layer, then you can reshape it.

Edit: It's probably going to be easier to just use skimage in the same fashion you used the deprecated scipy:

from skimage import transform,io
# read in grey-scale
grey = io.imread('your_image.png', as_grey=True)
# resize to 28x28
small_grey = transform.resize(grey, (28,28), mode='symmetric', preserve_range=True)
# reshape to (1,784)
reshape_img = small_grey.reshape(1, 784)
Mr K.
  • 1,064
  • 3
  • 19
  • 22