所以我正在使用图像的像素数据,对它应用一些计算,这给了我一个结果数组,其形状是未知的,直到计算完成为止 . 我无法将输出的阵列重新塑造成二维或三维阵列
这是一个例子
from PIL import Image
img = Image.open('C:\Users\Amit\Desktop\sample_pic.jpeg').convert("RGB")
pixels =np.array(img)
print(pixels.shape) # (477L, 887L, 3L) PIL seems to switch height and width because original dimensions are 877 x 477
flat = pixels.flatten()
print (flat.shape) # (1269297L,)
filter1= np.array([1,1,0])
pixels2 = np.array([])
for i in range(0, len(flat),2):
pixels2 =np.append(pixels2,np.sum((flat[i:i+3] * filter1)))
最后的循环只是对扁平化阵列进行一些计算,并输出一个新的阵列,其形状我不知道直到输出
print pixels2.shape
#(634649L,)
所以我试图将输出的阵列重新塑造成适合图片的尺寸 . 我试过了代码
pixels2.reshape(800,-1)
但我得到了错误
pixels2.reshape(800,-1)
ValueError: total size of new array must be unchanged
同样的
pixels.reshape(800,-1,3)
ValueError: total size of new array must be unchanged
我希望添加(-1)会自动找到合适的第二维,但似乎并非如此 . 我不会将数字800作为其中一个尺寸,但我正在寻找前两个尺寸超过300的尺寸(300,300,3)
谢谢 .
更新:
向 pixels2
数组添加一个元素,使其成为一个可被3整除的(634650L)数组 . (我通过反复试验找到了它)但是找到其他两个维度也涉及到大量的试验和错误 . (800,-1,3)不起作用 .