Python PIL命令

时间:2016-07-07 07:07:37

标签: python opencv python-imaging-library

我有一个使用opencv的脚本,但我在安装它时遇到了很多问题。作为替代方案,我想用PIL命令替换opencv命令。

要替换的命令:

im = cv2.resize(cv2.imread('/path/to/image'), (224, 224)).astype(np.float32)
im[:,:,0] -= 103.939
im[:,:,1] -= 116.779
im[:,:,2] -= 123.68
im = im.transpose((2,0,1))
im = np.expand_dims(im, axis=0) 

我尝试过:

f="/path/to/image"
image = Image.open(f)
new_width  = 224
new_height = 224
im = image.resize((new_width, new_height), Image.ANTIALIAS)
im=np.asarray(im)
#print(im)
im = np.expand_dims(im, axis=0)
im = np.array(im).copy()
im=np.float32(im)
#print(im[:,0])
#im = cv2.resize(cv2.imread('/path/to/image'), (224, 224)).astype(np.float32)
im[:,:,0] -= 103.939
im[:,:,1] -= 116.779
im[:,:,2] -= 123.68
#im = im.transpose((2,0,1))  #COMMENT 1
#print(im[0,0,0])
im = np.expand_dims(im, axis=0)
print(im)  #COMMENT 2

错误:

Exception: Error when checking : expected zeropadding2d_input_1 to have shape (None, 3, 224, 224) but got array with shape (1, 1, 224, 224)

如何将正确尺寸设为Nonex3x224x224? 3来自哪里?图像加载命令有什么区别吗?另外COMMENT 1命令的意义是什么?如果我取消注释,我会得到一个1x124x1x124的矩阵。

当我在im打印COMMENT 2时,我得到:

[[[[  91.06099701   78.22100067   71.31999969 ...,  101.          104.
     105.        ]
   [  91.06099701   78.22100067   71.31999969 ...,  102.          105.
     106.        ]
   [  90.06099701   77.22100067   70.31999969 ...,  104.          109.
     111.        ]
   ..., 
   [  33.06099701   21.22100067   16.31999969 ...,  209.          207.
     206.        ]
   [  29.06099701   17.22100067   12.31999969 ...,  209.          207.
     206.        ]
   [  28.06099701   16.22100067   11.31999969 ...,  209.          207.
     206.        ]]]]

0 个答案:

没有答案