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

发布于 2024-08-15 22:07:06 字数 144 浏览 1 评论 0原文

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

我找不到一个好的 API 来处理与 python 鼠标相关的东西。

I am very new to python. I am trying to write a program that will click the mouse at (x, y), move it to (a, b), and then wait until the color under the mouse is a certain color, lets say #fff. When it is that color, it clicks again and then repeats.

I cannot find a good API for mouse related stuff for python.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋凉 2024-08-22 22:07:06

用于模拟鼠标事件的 API 取决于您的平台。我不知道有什么跨平台的解决方案。

在 Windows 上,您可以通过 ctypes 访问 Win32 API。请参阅 MSDN 上的 mouse_event。您可能还对 pywinauto 感兴趣。

为了获取鼠标下的颜色,您需要鼠标位置。请参阅 MSDN 上的 GetCursorPos。然后,如果您的应用程序具有用于获取此位置颜色的 API,您就可以使用它。如果没有,您可以尝试抓取光标周围的一小部分屏幕并使用 PIL 用于获取该区域中每个像素的颜色。我认为 PIL 屏幕捕获仅适用于 Windows 平台,但我不确定。

我正在使用以下函数来满足类似的需求:

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")

The API for simulating mouse events depends on your platform. I don't know any cross-platform solution.

On Windows, you can access the Win32 API thanks to ctypes. see mouse_event on MSDN. You may also be interested by pywinauto

For getting the color under the mouse, you need the mouse position. See GetCursorPos on MSDN. Then if your app has an API for getting the color at this position you can use it. If not, you can try to grab a small portion of the screen around the cursor and to use PIL for getting the colors of every pixel in this area. I think that PIL screen capture is only working on Windows paltform but I am not sure.

I am using the following function for a similar need:

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")
扬花落满肩 2024-08-22 22:07:06

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

if you're on Windows, then, for this kind of thing, you really want to try autohotkey. It's not python, but it is extremely powerful for doing this kind of thing on a Windows machine. The user community is extremely helpful, also. Check out their "ask for help" forum.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文