代码之家  ›  专栏  ›  技术社区  ›  Logan Serman

在Python中模拟鼠标单击/检测光标下的颜色

  •  5
  • Logan Serman  · 技术社区  · 16 年前

    我对python非常陌生。我正在尝试编写一个程序,在(x,y)处单击鼠标,将其移动到(a,b),然后等待鼠标下的颜色为特定颜色,比如说#fff。当它是该颜色时,它会再次单击,然后重复。

    2 回复  |  直到 16 年前
        1
  •  8
  •   luc    16 年前

    模拟鼠标事件的API取决于您的平台。我不知道任何跨平台的解决方案。

    mouse_event on MSDN pywinauto

    要获得鼠标下的颜色,需要鼠标位置。看见 GetCursorPos on MSDN PIL

    def grab_main_color(self, rect, max_colors=256):
        """returns a tuple with the RGB value of the most present color in the given rect"""
        img=ImageGrab.grab(rect)
        colors = img.getcolors(max_colors)
        max_occurence, most_present = 0, 0
        try:
            for c in colors:
                if c[0] > max_occurence:
                    (max_occurence, most_present) = c
            return most_present
        except TypeError:
            raise Exception("Too many colors in the given rect")
    
        2
  •  -1
  •   reckoner    16 年前

    如果你在Windows上,那么,对于这类事情,你真的想试试 autohotkey . 它不是python,但它是 极其强大 在Windows计算机上执行此类操作。用户社区也非常有帮助。查看他们的“寻求帮助”论坛。