将2D灰度PIL图像转换为1D numpy数组后,获得2D numpy数组(图像/矩阵)的最平滑方法是什么?

时间:2015-05-27 23:34:32

标签: python arrays numpy python-imaging-library

我的代码如下所示:

img = Image.open(path)
pix = np.asarray(img)
# i tried to access an (x,y) pixel and found the array was one dimensional
print str(float(pix[1,1])), "\t",

我需要将1D数组洗牌以使其为2D,这样我就可以访问(x,y)像素。最顺畅的方法是什么?

我上面得到了这个错误:

IndexError: too many indices for array

修改

以下是执行上述代码后从终端收集的一些信息。 np.asarray()显然正在做某事,有关图像维度的信息仍然包含在ndarray pix - array(<PIL.TiffImagePlugin.TiffImageFile image mode=I;16 size=1280x1080 at 0x110CA9F38>, dtype=object)中。

>>> img
<PIL.TiffImagePlugin.TiffImageFile image mode=I;16 size=1280x1080 at 0x110CA9F38>
>>> pix
array(<PIL.TiffImagePlugin.TiffImageFile image mode=I;16 size=1280x1080 at 0x110CA9F38>, dtype=object)
>>> type(img)
<type 'instance'>
>>> type(pix)
<type 'numpy.ndarray'>
>>> img.size
(1280, 1080)
>>> pix.size
1

1 个答案:

答案 0 :(得分:1)

如果你可以使用scipy:

from scipy import ndimage
image = ndimage.imread('image.png')
image.shape
相关问题