1
我正在处理图像生成程序,并且尝试直接编辑图像的像素时出现问题。Python PIL编辑像素与ImageDraw.point
我原来的方法,它的工作原理,很干脆:
image = Image.new('RGBA', (width, height), background)
drawing_image = ImageDraw.Draw(image)
# in some loop that determines what to draw and at what color
drawing_image.point((x, y), color)
这工作得很好,但我认为直接修改的像素可能会稍快一些。我打算使用“非常”高分辨率(可能是10000px x 10000px),所以即使每个像素的时间略有下降也会大幅下降。
我尝试使用这样的:
image = Image.new('RGBA', (width, height), background)
pixels = image.load()
# in some loop that determines what to draw and at what color
pixels[x][y] = color # note: color is a hex-formatted string, i.e "#00FF00"
这给了我一个错误:
Traceback (most recent call last):
File "my_path\my_file.py", line 100, in <module>
main()
File "my_path\my_file.py", line 83, in main
pixels[x][y] = color
TypeError: argument must be sequence of length 2
如何实际pixels[x][y]
工作?我似乎错过了一个基本概念(我从来没有在这之前直接编辑像素),或者至少只是不理解需要什么参数。我甚至尝试过pixels[x][y] = (0, 0, 0)
,但是也提出了相同的错误。
另外,有没有更快的编辑像素的方法?我听说使用pixels[x][y] = some_color
比绘制图像更快,但我愿意接受任何其他更快的方法。
在此先感谢!