Heatsreef
Heatsreef

Reputation: 119

Py: imageio.imread vs scipy.misc.imread

So, i have a script that should convert a number into an array that i can feed into my AI. But a book that I am using for that tells me, to do so:

img_array = scipy.misc.imread("picofannumber.png", flatten = True)
img_data = 255.0 - img_array.reshape(784)

So, does not work, i guess my libraries are too outdated, because that does not work. So I am using this now:

img_array = imageio.imread("picofannumber.png", as_gray = True)   
img_data = 255.0 - img_array.reshape(784) 

But then my problem is:

ValueError: cannot reshape array of size 361928 into shape (784,)

I also tried

img_array = imageio.imread("picofannumber.png", as_gray = True)   
img_data = 255.0 - img_array.reshape(28,28) 

but it won't work either, same error.

Upvotes: 0

Views: 2334

Answers (1)

Heatsreef
Heatsreef

Reputation: 119

So for everyone who just stumbled upon this post:

img_array.reshape(784) does only resize an "numpy-array" and does not resize the picture how i thought.

The updated (working) code:

import imageio
from PIL import Image

Image.open("picofannumber.png").resize((28,28),Image.LANCZOS).save("picofannumber.png")
    
img_array = imageio.imread("picofannumber.png", as_gray=True)
img_data  = 255.0 - img_array.reshape(784)

Upvotes: 2

Related Questions