user3320033
user3320033

Reputation: 245

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

Upvotes: 4

Views: 42167

Answers (2)

slongfield
slongfield

Reputation: 101

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

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

Upvotes: 4

doug
doug

Reputation: 70028

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

Upvotes: 9

Related Questions