使用 python Pillow 中的列表生成图像不起作用

Generating a image with a List in python Pillow doesnt work

我最近开始在一些项目中使用 pillow,但我无法生成带有列表对象的图像。列表中的每个整数都有一个介于 0 和 255 之间的值。 所以这是我的相关代码:

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

即使我将像素中的每个元素都更改为 0,输出始终是全黑图片。 我也尝试使用 'RGB' 模式而不是 'L' 模式,但随后出现此错误:

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

我不太理解这个错误,我还更改了列表,以便它将所有 3 个 RGB 值保存为元组。

知道问题出在哪里吗?

提前致谢!

这样使用:

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

然后你得到:

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

意味着您应该像“(R,G,B)”一样使用 rgb (a tuple)。