2014-04-01 9 views
2

一個RGB矩陣圖像我有一個RGB矩陣是這樣的:顯示在python

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)], 
     .........., 
     [()()()()] 
     ] 

我想顯示包含上述矩陣的圖像。

我使用該功能,以顯示灰階圖像:

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

參數(圖像)具有矩陣

人幫助我如何顯示RGB矩陣

回答

1

Matplotlib的內置函數imshow會讓你做到這一點。

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

此代碼執行時沒有錯誤,但不會顯示任何錯誤 – user3320033

+0

如果您未使用matplotlib保存圖像,但想要打開圖像,則需要添加「plt.show()」。我編輯了原來的答案來反映這一點。 – slongfield

3

imshow在matplotlib圖書館將做的工作

什麼是關鍵的是,你與NumPy陣列具有正確的形狀:

高x寬x 3

(或RGBA的高度×寬度×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()