2016-08-22 59 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

谢谢!这解决了问题! 我的错误是,我使用了一个二维列表 –