在python中显示rgb矩阵图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22777660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:39:42  来源:igfitidea点击:

display an rgb matrix image in python

pythonimagematlabmatrixrgb

提问by user3320033

I have an rgb matrix something like this:

我有一个像这样的 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)],
       ..........,
       [()()()()]
      ]

i want to display an image which contains the above matrix.

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

I used this function to display a gray-scale image:

我用这个函数来显示灰度图像:

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

argument(image)has the matrix

参数(图像)具有矩阵

anybody help me how to display the rgb matrix

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

回答by slongfield

Matplotlib's built in function imshow will let you do this.

Matplotlib的内置函数 imshow 可以让你做到这一点。

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

回答by doug

imshowin the matplotlib library will do the job

matplotlib 库中的imshow将完成这项工作

what's critical is that your NumPy array has the correct shape:

关键是您的 NumPy 数组具有正确的形状:

height x width x 3

高 x 宽 x 3

(or height x width x 4 for RGBA)

(或 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()