The code below is supposed to find the co-ordinates of a button on the screen. It runs but it's way too slow. It takes almost a minute for each iteration of x.

I'm assuming it's because, in the algorithm that goes through each row and column(or x and y), in the find_show_button() function, it recaptures the entire screen over every iteration. Some googling also suggests that screen capturing is usually slow. So how do I make the algo capture one image and use that same image until a result is found?

Although I may be wrong so any other insight are also appreciated.

from ctypes import windll im = windll.user32.GetDC(0) def getpix(x,y): return windll.gdi32.GetPixel(im,x,y) def find_show_button(): xy = [] for x in range(1680): for y in range(1050): if getpix(x, y) == 1343713 and getpix(x+134, y) == 1343713: xy.append(x+67) xy.append(y) break if len(xy) == 2: return xy return False