0

I am using a infinite loop constantly scanning a X,Y place at my screen waiting for the color to change (I need it to be as fast as possible, as soon as the color changes), then send a key press but this is making my computer lag a lot, any tips on how to improve my code? I already tried using more time.sleeps but doesn't helped that much

import time
import pyautogui
from PIL import Image, ImageGrab
x = 100
y = 100
color = ImageGrab.grab().getpixel((x, y))
repeat = True
while repeat:
    time.sleep(0.05)
    newcolor = ImageGrab.grab().getpixel((x, y))
    if newcolor != color:
        pyautogui.press('b')
        time.sleep(0.1)
martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

If you could find the frame rate that your computer refreshes the screen at, lets say 60 frames per second, then you could set the time.sleep so that the program only checks 60 times per second. If it was 60, your time.sleep might look like so:

time.sleep(1 / framesPerSecond)

-707