How to get the color of a pixel in python as fast as possible?

0

I'm on ubuntu and I want to check every 0.1sec the color of a specific pixel on my screen. How can I do that?

I know about PIL but this would need a full screenshot every 0.1sec just for one single pixel.

Then I found this method using ctypes.windll : Faster method of reading screen pixel in Python than PIL?

But this wont work because I'm not on Windows. Any other idea?

Edit: Solved thanks to b_c

from Xlib import display, X
from PIL import Image #PIL


def getColor(x,y):
    W, H = 1, 1
    dsp = display.Display()
    root = dsp.screen().root
    raw = root.get_image(x, y, W, H, X.ZPixmap, 0xffffffff)
    image = Image.frombytes("RGB", (W, H), raw.data, "raw", "BGRX")
    print image.getpixel((0, 0))
    time.sleep(0.01)
python
python-2.7
image-processing
asked on Stack Overflow Nov 7, 2019 by noxus Nexus • edited Nov 7, 2019 by noxus Nexus

1 Answer

3

PIL and other similar programs usually allow you to specify a boundary box to grab smaller amounts

PyAutoGui allows you to take smaller sections

as referenced here https://pyautogui.readthedocs.io/en/latest/screenshot.html code such as

pyautogui.screenshot(region=(0,0, 300, 400))

could be useful

https://pillow.readthedocs.io/en/4.2.x/reference/Image.html

could be useful as well, bbox allows you to only observe a small area.

answered on Stack Overflow Nov 7, 2019 by AlbinoRhino • edited Nov 7, 2019 by martineau

User contributions licensed under CC BY-SA 3.0