I'm trying to write a script in python which searches the screen for a specific color in RGB, gets the pixel coordinates and then sends those to a click function to click on it. Here's my code so far:
from PIL import ImageGrab
import win32api, win32con
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
color = (10,196,182)
session = False
match = False
while(True):
screen = ImageGrab.grab()
found_pixels = []
for i, pixel in enumerate(screen.getdata()):
if pixel == color:
match = True
found_pixels.append(i)
break
else:
match = False
width, height = screen.size
found_pixels_coords = [divmod(index, width) for index in found_pixels]
if session == False and match == True:
click(found_pixels_coords)
print("Match_Found")
session = True
if session == True and match == False:
session = False
How can I convert the output of founds_pixels_coords to use it inside the click(x,y) function? I'm also getting the output values reversed, (y,x) instead of (x,y), which I don't understand why.
Here's my console output, in case I'm totally wrong:
Traceback (most recent call last):
File "test2.py", line 28, in <module>
click(found_pixels_coords)
TypeError: click() missing 1 required positional argument: 'y'
EDIT:
click(*found_pixels_coords[0]) as suggested by @martineau seems to solve the missing argument error. I also got around the reversed values by defining click(y,x) . Any proper solution to this however, would be greatly appreciated.