Python获取图像矩阵PIL

11
我正在尝试加载一张图片,将其转换并打印出矩阵。我有以下代码;
im = Image.open("1.jpg")
im = im.convert("L")
print im

当我打印“im”时,会出现以下内容:<PIL.Image.Image image mode=L size=92x112 at 0x2F905F8>。我怎样才能看到图像矩阵?
3个回答

17
你可以使用 numpy.asarray() 函数:
>>> import Image, numpy
>>> numpy.asarray(Image.open('1.jpg').convert('L'))

5

使用load函数可以像这样访问像素:

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

2

im.show()会在弹出窗口中显示图像。

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

im.save()可将图像保存到文件中。


im.tostring() is now replaced by im.tobytes() - Shashwat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接