I have a python script that takes screenshots of some parts of the screen and if the arranged RGB code matches, clicks on the object. It works well on not moving objects. However, when the object moves, it can not click on it due to delay. I do not know the direction of the object, it is random. Therefore, I can not solve the problem by rearranging the click coordinates.
Do you have any advice? Is there a better way to write this script? Is there any trick to click faster the found coordinates or find the desired coordinates more quickly? Object recognization does not work well due to background.
Thank you!
import pyautogui
import keyboard
import time
import win32api, win32con
time.sleep(2)
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
while 1:
pic = pyautogui.screenshot(region=(265,220,200,200))
width, height = pic.size
for x in range(0,width,10):
for y in range(0,height,10):
r, g, b = pic.getpixel((x,y))
if g in range(90,95):
click(x+265,y+220)
time.sleep(1)
break
Some suggestions to try:
roi = (265,220,200,200)
width, height = roi[2]-roi[0], roi[3]-roi[1]
range_x = range(0,width,10)
range_y = range(0,height,10)
while 1:
pic = pyautogui.screenshot(region=roi)
img_g = np.array(pic)[:,:,1] # get the green channel
g_in_range = (95<img_g)&(90<img_g)
for x in range_x :
for y in range_y :
g = img_g((y,x)) # double check x,y order
if g:
click(x+265,y+220)
time.sleep(1)
break
If you need even more speedup:
Try following this article about using cython
.