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

标签 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",

我需要打乱一维数组,使其成为二维数组,以便我可以访问 (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

最佳答案

如果你可以使用scipy:

from scipy import ndimage
image = ndimage.imread('image.png')
image.shape

关于python - 在我将 2D 灰度 PIL 图像转换为 1D numpy 数组后,获得 2D numpy 数组(图像/矩阵)的最平滑方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30494869/

相关文章:

python - pandas 系列上的 numpy diff

Python 和正则表达式

python - PyQt4中QWebView的多线程

python - SQLAlchemy 一对一关系创建多行

python - 在Python中循环和计算json响应

java - 如何显示数组的元素?

c - 结构体中的数组并填充他

python - Sklearn SVM系数属性-如何获取类名?

java - 遍历两个数组以检查是否相等

python - 在python中将numpy.datetime64转换为字符串对象