0

I want to detect cursor position and colour quickly (less than 0.1 sec)

I read this article and want to try he's last method (Xlib)

However, when I run os.getenv("DISPLAY"), it returns None

Does anyone know how to fix this or offer me a new method to achieve my goal?

System: MacBook Pro, macOS 10.15.4

my name
  • 51
  • 5

2 Answers2

1

Xlib is usually not available on macOS. You may consider using a library such as pynput or pyautogui to achieve what you want to do instead.

e.g. with pynput

from pynput.mouse import Controller

mouse = Controller()
print(mouse.position)  # (x, y) coordinates tuple 
michaeldel
  • 2,204
  • 1
  • 13
  • 19
  • Thanks for answering! My main goal is actually to get the colour at a position of the screen. MacBook has a high-resolution screen, which means it takes a long time to take a screenshot (0.3s). So I am looking for a better way to do it. – my name Apr 16 '20 at 02:42
  • This time can be vastly improved as explained here https://stackoverflow.com/a/13024603/1812262 – michaeldel Apr 16 '20 at 02:43
1

Pyautogui is good library for GUI operations its having position() and i used screenshot() and then get color of given pixel, this is something i have tried, you can check it out by this below code,

Before go with code, install package by using

python -m pip install pyautogui

Code:

import pyautogui

try:
    while True:
        x, y = pyautogui.position()
        pixelColor = pyautogui.screenshot().getpixel((x, y))
        screenShot = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
        screenShot += ' RGB: (' + str(pixelColor[0]).rjust(3)
        screenShot += ', ' + str(pixelColor[1]).rjust(3)
        screenShot += ', ' + str(pixelColor[2]).rjust(3) + ')'
        print(screenShot)

except KeyboardInterrupt:
    print("\nDone...")