Search code examples
pythonpyautogui

pyautogui.pixel(x,y) starts lagging in a while loop


I want to check when a pixel on the screen is changing and then put a keyboard event when the pixel is changing colors. But after about 2-4 min the computer slows down when pyautogui.pixel() have been called multiple times.

Here is my code

import pyautogui

OldC1 = None

while True:
   NewC1 = pyautogui.pixel(750, 550)

   if NewC1 != OldC1:
      pyautogui.press('up')

   OldC1 = NewC1;

Is there a way to get around the lag?


Solution

  • I've encountered similar issues, my way of circumventing that problem was to use PIL's functions directly, instead of pyautogui's wrapper functions:

    from PIL import ImageGrab
    
    pixelRGB = ImageGrab.grab().getpixel((x, y))
    

    Using this function did not slow down my pc.