1

I am trying to make a bot that will play an online game for me. To do this I need to take a screenshot and see if there is a specific colour in a specific location. The game requires this to be done as quickly as possible, and the code below is not fast enough as it can only complete roughly 7 times a second. Does anyone know of a better way to optimise this code to make it run faster?

import imageio
import pyautogui
run = True
while run:
    myScreenshot = pyautogui.screenshot()
    myScreenshot.save(r'location\answers.png')
    pic = imageio.imread('answers.png')
    red_pixel_col = pic[500, 163]
    if red_pixel_col[0] == 208:
        print('yes')
    else:
        print('no')

P.S You would need to replace location in the save line (line 6) with a valid path

HansHirse
  • 18,010
  • 10
  • 38
  • 67
BarryBBenson
  • 71
  • 1
  • 6
  • If screenshotting is the bottleneck, perhaps you could try different screenshot utilities. And capturing only a particular region of the screen instead of the whole screen. (e.g. just a single pixel, in this case.) – Mateen Ulhaq Jul 23 '20 at 07:32
  • You're searching only at that specific pixel? Then why screenshot the whole screen and not just that part. – ZWang Jul 23 '20 at 07:32
  • 1
    you can also look into skipping the save on disc/reload part - IO is slow. Read about BytesIO to keep the data in memory - f.e. here: https://stackoverflow.com/questions/646286/how-to-write-png-image-to-string-with-the-pil – Patrick Artner Jul 23 '20 at 07:42

0 Answers0