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
比繪製圖像更快,但我願意接受任何其他更快的方法。
在此先感謝!