派:imageio.imread 与 scipy.misc.imread

Py: imageio.imread vs scipy.misc.imread

所以,我有一个脚本可以将一个数字转换成一个数组,我可以将其输入到我的 AI 中。但是我正在使用的一本书告诉我,这样做:

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

所以,不起作用,我想我的库太过时了,因为那不起作用。所以我现在用这个:

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

但是我的问题是:

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

我也试过了

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

但是也不行,同样的错误。

所以对于刚刚偶然发现这个 post 的每个人:

img_array.reshape(784) 只调整了“numpy-array”的大小,并没有按照我的想法调整图片的大小。

更新的(工作)代码:

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)