Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display an rgb matrix image in python

I have an rgb matrix something like this:

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

like image 377
user3320033 Avatar asked Dec 12 '22 06:12

user3320033


1 Answers

imshow in the matplotlib library will do the job

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

height x width x 3

(or height x width x 4 for RGBA)

>>> 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()
like image 162
doug Avatar answered Dec 13 '22 21:12

doug