4

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

2 Answers 2

9

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()
Sign up to request clarification or add additional context in comments.

Comments

4

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

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

3 Comments

this code executes without error but doesn't show me any error
If you aren't saving the image with matplotlib, but want to open the image, you need to add in "plt.show()". I've edited the original answer to reflect this.
This has a strang white bounding box and also axis

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.