Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将图像的像素打印为矩阵?_Python_Image_Python Imaging Library_Pixels - Fatal编程技术网

Python 如何将图像的像素打印为矩阵?

Python 如何将图像的像素打印为矩阵?,python,image,python-imaging-library,pixels,Python,Image,Python Imaging Library,Pixels,我想把图像的像素打印成矩阵 以下是我正在使用的代码: from PIL import Image im = Image.open("8Black.png") pixels = list(im.getdata()) print(pixels) 我使用photoshop创建了图像“8Black.png”,并将其全部涂成黑色。我使用的python版本是3.5.0。但是,当我运行上述代码时,我得到: [0,0,0,0,0,0,0,0,0,0,0,0,...,0] 我想换成这个: [[0,0,0,

我想把图像的像素打印成矩阵

以下是我正在使用的代码:

from PIL import Image

im = Image.open("8Black.png")
pixels = list(im.getdata())

print(pixels)
我使用photoshop创建了图像“8Black.png”,并将其全部涂成黑色。我使用的python版本是3.5.0。但是,当我运行上述代码时,我得到:

[0,0,0,0,0,0,0,0,0,0,0,0,...,0]
我想换成这个:

[[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[...],[...]]
我试图改变这一点:

pixels = list(im.getdata())
为此:

pixels = np.matrix(im.getdata())
但还是没有得到我想要的结果


如何获得矩阵形式的像素数据?我遗漏了什么吗?

您明确要求通过执行
getdata()
将原始2D图像“展平”为1D序列

您可以使用
重塑
将其恢复为原始形式

matrix = np.array(im.getdata()).reshape(im.size)

@谢谢你编辑我的问题。对不起,我英语不好。没问题。欢迎使用堆栈溢出,顺便说一下:)