为什么将PIL.Image.open()函数对象的尺寸转换为numpy数组后会发生变化

时间:2018-10-18 18:59:09

标签: python python-2.7 python-imaging-library

我正在尝试使用Pillow模块读取图像,如以下命令所示:

import PIL
import numpy as np
img = PIL.Image.open('/home/wd/foo.jpg').convert('L')
img.size
(247, 249)

但是,当我将其转换为用于图像处理的numpy数组时,尺寸会交换/更改。

img1 = np.array(img)
img1.shape
(249, 247)

我试图在文档中找到解决方案,但找不到它。

我在Linux机器上使用的是python 2.7.13,Pillow(PIL)版本5.1.0。

1 个答案:

答案 0 :(得分:1)

从PIL documentation

  

PIL.Image.size

     

图像大小,以像素为单位。大小以2元组(宽度,高度)给出。

但是,numpy.ndarray.shape只是以其他顺序(高度,宽度)返回形状