我希望能够创建 Tkinter Canvas 并与之交互,并且在任何时候都能够迭代它的每个像素并获取它们的 RGB 值。 不需要逐个像素地设置,只是获取。但是,类似于 Canvas 的 create_polygon()、create_line()、create_text() 和 create_oval() 的方法也必须可用,以便与整体图像进行交互。
有很多限制:
- 必须使用 Python 3
- 必须使用 Linux、Mac 和 Windows
- 必须使用 Python 附带的库(无需下载)
第二个限制主要是我在 getting the color of pixels on the screen in Python3.x 时发布这个问题的原因。以及其他几个类似的问题已经存在。
如果这是不可能的,我能得到的最接近的是什么?
最佳答案
尝试一下。但是很慢:/
from util.color import Color
class ImageUtils:
@staticmethod
def get_pixels_of(canvas):
width = int(canvas["width"])
height = int(canvas["height"])
colors = []
for x in range(width):
column = []
for y in range(height):
column.append(ImageUtils.get_pixel_color(canvas, x, y))
colors.append(column)
return colors
@staticmethod
def get_pixel_color(canvas, x, y):
ids = canvas.find_overlapping(x, y, x, y)
if len(ids) > 0:
index = ids[-1]
color = canvas.itemcget(index, "fill")
color = color.upper()
if color != '':
return Color[color.upper()]
return "WHITE"
关于python-3.x - 获取 tkinter Canvas 的像素颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28014347/