我的代码如下所示:
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
答案 0 :(得分:1)
如果你可以使用scipy:
from scipy import ndimage
image = ndimage.imread('image.png')
image.shape