如何检测鼠标光标直接位于的特定像素的颜色(对于屏幕上的任何位置)
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以调用
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 callx, y = pyautogui.position()
to obtain the x, y coordinates, then callpyautogui.moveRel(50, 0)
to move the mouse cursor away, then callpyautogui.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.