2016-08-22 62 views
1

我最近開始使用枕頭某些項目,但我無法設法生成帶有列表對象的圖像。列表中的每個INT的值爲0到255之間,並 所以這是我的相關代碼:使用python枕頭列表生成圖像不起作用

img = Image.new('L',(width,height)) 
img.putdata(pixel) 
img.save('img.png') 

輸出始終是一個全黑的畫面,甚至當我在像素的每一個元素改爲0 我使用的「L」模式「RGB」模式istead也嘗試過,但後來我得到這個錯誤:

SystemError: new style getargs format but argument is not a tuple

我真的不與已瞭解的錯誤,我也改變了列表,以便它擁有所有3 RGB值作爲元組。

任何想法可能是什麼問題?

在此先感謝!

回答

2

使用這樣的:

from PIL import Image 

pixel = [] 
for i in range(300*100): 
    pixel.append((255,0,0)) 
for i in range(300*100): 
    pixel.append((0,255,0)) 
for i in range(300*100): 
    pixel.append((0,0,255)) 
img = Image.new('RGB',(300,300)) 
img.putdata(pixel) 
img.show() 

然後你得到:

enter image description here

SystemError: new style getargs format but argument is not a tuple

意味着你應該使用RGB像 「(R,G,B)」(一個tuple )。

+0

謝謝!這解決了問題! 我的錯誤是,我使用了一個二維列表 –