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

2 Answers 2

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 
Sign up to request clarification or add additional context in comments.

2 Comments

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.
This time can be vastly improved as explained here stackoverflow.com/a/13024603/1812262
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...")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.