Python获取图像矩阵PIL

Ror*_*ter 7 python numpy python-imaging-library

我试图加载图像,转换它并打印矩阵.我有以下代码;

im = Image.open("1.jpg")
im = im.convert("L")
print im
Run Code Online (Sandbox Code Playgroud)

当我打印'即时'我得到这个<PIL.Image.Image image mode=L size=92x112 at 0x2F905F8>.我怎样才能看到图像矩阵?

Ble*_*der 12

你可以使用numpy.asarray():

>>> import Image, numpy
>>> numpy.asarray(Image.open('1.jpg').convert('L'))
Run Code Online (Sandbox Code Playgroud)


Mat*_*euW 5

函数加载将允许您访问像这样的像素:

b = im.load()
print b[x,y]
b[x,y] = 128    # or a tupple if you use another color mode
Run Code Online (Sandbox Code Playgroud)


mha*_*wke 2

im.show()将在弹出窗口中显示它。

im.tostring()会将图像转储为字节字符串。

im.save()将其保存到文件中。