获取 tkinter 画布的像素颜色 [英] Get pixel colors of tkinter canvas

查看:29
本文介绍了获取 tkinter 画布的像素颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够创建 Tkinter 画布并与之交互,并且能够随时迭代其每个像素并获取它们的 RGB 值.逐像素设置不是必需的,只是获取.但是,与 Canvas 的 create_polygon()、create_line()、create_text() 和 create_oval() 类似的方法也必须可用于与整个图像进行交互.

I'd like to be able to create and interact with a Tkinter Canvas and, at any time, be able to iterate over each of its pixels and get their RGB values. Setting pixel by pixel is not necessary, just getting. However, methods analogous to Canvas's create_polygon(), create_line(), create_text(), and create_oval() must be available as well for interacting with the image overall.

有许多限制:

  • 必须使用 Python 3
  • 必须使用 Linux、Mac 和 Windows
  • 必须使用 Python 附带的库(无需下载)

第二个限制主要是我在 在 Python3.x 中获取屏幕上像素的颜色 和其他几个类似的问题已经存在.

The second restraint is mainly the reason I've posted this question when getting the color of pixels on the screen in Python3.x and several other similar questions already exist.

如果这是不可能的,我能得到的最接近的是什么?

If this is impossible, what is the closest I can get?

推荐答案

尝试一下.但是很慢:/

Try it. But is slow :/

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"

这篇关于获取 tkinter 画布的像素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆