1. 程式人生 > >python報錯:ValueError: 'arr' does not have a suitable array shape for any mode.

python報錯:ValueError: 'arr' does not have a suitable array shape for any mode.

首先預設import scipy as sp。

一般來說,是遇到了sp.misc.imsave或者sp.misc.imresize出現了該類問題。

一、imsave遇到了該問題

根據scipy的imsave官方文件,我們可以知道,引數中的arr

arr : ndarray, MxN or MxNx3 or MxNx4

Array containing image values. If the shape is MxN, the array represents a grey-level image. Shape MxNx3 stores the red, green and blue bands along the last dimension. An alpha layer may be included, specified as the last colour band of an MxNx4

 array.

分別對應單一灰度通道,RGB三通道,RGB+alpha四通道。不過大部分人應該只會用到前兩個(其實是因為博主嘗試了sp.misc.imsave去儲存四通道的影象失敗了哈哈,不管這麼多了)

所以說,如果你想儲存的影象維度不符合以上三種(一般是前兩種)預設方式的話,就會報這樣的錯!

(博主遇到的問題是想以 M x N x 1 的方式儲存單通道的圖,所以就報錯啦)

二、imresize遇到了該問題

sp.misc.imresize(img, [img_size_1, img_size_2])

請注意img一定需要是numpy陣列哦。博主就是在這個地方沒有用規定格式於是報了該錯誤,記得去檢視一下。