如何检测鼠标光标直接位于的特定像素的颜色(对于屏幕上的任何位置)

发布于 2025-01-14 07:05:28 字数 86 浏览 1 评论 0原文

我正在尝试使用 python 代码和函数“pyautogui”来自动化一些工作,但我需要找到一种方法来检测鼠标在屏幕上的颜色。 有人有解决办法吗? 提前致谢。

I am trying to automate some work using python code and the function "pyautogui" but I need to find a way to detect colours where the mouse is on the screen.
Anyone has any solutions?
Thanking in advance.

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

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

发布评论

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

评论(2

锦上情书 2025-01-21 07:05:28
import pyautogui
while True:
    x, y = pyautogui.position()
    px = pyautogui.pixel(x, y)
    print(px)
import pyautogui
while True:
    x, y = pyautogui.position()
    px = pyautogui.pixel(x, y)
    print(px)
陈甜 2025-01-21 07:05:28

您可以调用pyautogui.pixel(x, y)来获取鼠标所在像素的(red, green, blue)值。不幸的是,在 macOS 上,鼠标光标本身包含在屏幕截图中。您必须调用 x, y = pyautogui.position() 来获取 x, y 坐标,然后调用 pyautogui.moveRel(50, 0) 来移动鼠标光标离开,然后调用 pyautogui.pixel(x, y) 来获取鼠标光标所在像素的颜色。

这有望在 PyAutoGUI 的未来版本中在 macOS 上得到纠正。

You can call pyautogui.pixel(x, y) to obtain the (red, green, blue) value of the pixel where the mouse is. On macOS unfortunately, the mouse cursor itself is included in the screenshot. You will have to call x, y = pyautogui.position() to obtain the x, y coordinates, then call pyautogui.moveRel(50, 0) to move the mouse cursor away, then call pyautogui.pixel(x, y) to get the color of the pixel of where the mouse cursor was.

This will hopefully be corrected on macOS in a future version of PyAutoGUI.

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