在python中显示rgb矩阵图像

时间:2014-04-01 05:39:20

标签: python image matlab matrix rgb

我有一个像这样的rgb矩阵:

image=[[(12,14,15),(23,45,56),(,45,56,78),(93,45,67)],
       [(r,g,b),(r,g,b),(r,g,b),(r,g,b)],
       [(r,g,b),(r,g,b),(r,g,b),(r,g,b)],
       ..........,
       [()()()()]
      ]

我想显示包含上述矩阵的图像。

我使用此功能显示灰度图像:

def displayImage(image):
    displayList=numpy.array(image).T
    im1 = Image.fromarray(displayList)
    im1.show()

参数(图像)具有矩阵

任何人都帮我如何显示rgb矩阵

2 个答案:

答案 0 :(得分:5)

matplotlib库中的

imshow 将完成这项工作

关键是你的NumPy阵列具有正确的形状:

高x宽x 3

(或RGBA的高度x宽度x 4)

>>> import os
>>> # fetching a random png image from my home directory, which has size 258 x 384
>>> img_file = os.path.expanduser("test-1.png")

>>> from scipy import misc
>>> # read this image in as a NumPy array, using imread from scipy.misc
>>> M = misc.imread(img_file)

>>> M.shape       # imread imports as RGBA, so the last dimension is the alpha channel
    array([258, 384, 4])

>>> # now display the image from the raw NumPy array:
>>> from matplotlib import pyplot as PLT

>>> PLT.imshow(M)
>>> PLT.show()

答案 1 :(得分:2)

Matplotlib内置函数imshow将允许您执行此操作。

import matplotlib.pyplot as plt
def displayImage(image):
    plt.imshow(image)
    plt.show()