Fav*_*las 5 python python-imaging-library
我在 Python 中使用 PIL 和 Image 模式。我想用这个代码创建一个图像:
imagesize = (12,12)
image = Image.new("RGB",imagesize,color=None)
Run Code Online (Sandbox Code Playgroud)
我将使用此函数在我的图像上放置像素:
.putpixel(xy, color)
Run Code Online (Sandbox Code Playgroud)
颜色在元组列表中。举个例子:
RGB = [((255, 255, 255),(207, 103, 36),(204, 93, 21),(204, 93, 21),(204, 93, 21), (.......some more RGB tuples.....)]
Run Code Online (Sandbox Code Playgroud)
我需要一个在 .putpixel(xy, color) 中的循环:
颜色每次递增一级。例如RGB[0],下一个循环RGB[1]等等。当这个循环被制作时,x 和 y 对我来说更困难。x 从 1 到 12(图像大小),而 y 为 0,然后,当 x 达到 imagesize 时,它返回到 1 到 12,但 y 现在为 1。当 x 和两者都达到图像大小的末尾时,循环结束。
谁能帮我?我是 Python 新手。
问候,
法沃拉斯
编辑
P:S - 忘了说,因为这是一个学校项目,我不能使用除了 img.new、img.show 和 img.outpixel 之外的任何方法
好吧,我上面的评论应该真的成为一个答案。我该睡一会儿了。您基本上可以立即将像素数据放入图像中。PIL.Image 的方法putdata接受组成图像像素的元组列表。所以:
img = PIL.Image.new("RGB", (12, 12))
img.show() # see a black image
pixels = [(255,0,0)]*(12*12)
img.putdata(pixels)
img.show() # see a red image
Run Code Online (Sandbox Code Playgroud)