2017-04-19 52 views
0

我有一個numpy數組(data.npy),它存儲了多個示例圖像。我想查看/保存所有圖像。我曾嘗試以下操作:顯示來自一個numpy陣列的多個圖像

img_array=np.load('data.npy') 
i = 0 
while i < len(img_array): 
    plt.imshow(img_array[i], cmap='gray') 
    plt.show() 
    i += 1 

但是這給出了一個錯誤:

TypeError: Invalid dimensions for image data 
+0

你有什麼是一個二進制文件,而不是一個數組。如何從中提取數據取決於文件創建時的存儲方式。 'img_array'的形狀是什麼? –

+0

@ChrisMueller img_array的形狀是:(22,4,100,100) –

回答

0

根據@John Zwinck的回答,以下代碼似乎對我來說工作得很好。

In [12]: for idx, el in enumerate(img_array): 
    ...:  plt.imshow(np.moveaxis(img_array[idx], 0, -1), cmap='gray') 
    ...: 

np.moveaxis圍繞陣列的軸移動。在這裏它將原始數組中的第一個軸移動到最後一個軸。

In [13]: a[10].shape 
Out[13]: (4, 100, 100) 

In [14]: np.moveaxis(a[10], 0, -1).shape 
Out[14]: (100, 100, 4) 
+0

感謝您的解釋作品吧! –