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

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

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

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

Need Your Help

IE9 raphael animation issue

javascript animation internet-explorer-9 raphael doctype

I have problem with display of correct javascript in IE9. Other browsers (Firefox, Opera, Chrome, Safari) work well, but animation in IE is not fluent. For example see this line which can be dragge...

Google App Engine ImportError

python google-app-engine web-applications python-2.7

I've come to run my app in development locally this morning, in the Google App Engine Launcher for Windows. Up until yesterday this has been working fine, and the code hasn't been changed since th...