How can I properly convert a list's output values to use them as args inside a function?

Viewed 82

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.

3 Answers

Since found_pixels_coords is a list (although it will never contain more than one set of coordindates), here's how to use the one set in it (if any matched):


    .
    .
    .
    if session == False and match == True:
        click(*found_pixels_coords[0]) # <== Do it like this.
        print("Match_Found")
        session = True
    if session == True and match == False:
        session = False
    .
    .
    .

Just call click() like this (add a * before the list name to unpack the values).

click(*found_pixels_coords)  # Make sure list contains 2 values

If you are not sure about number of items in found_pixels_coords list then just change the signature of click() function from

def click(x, y):

to

def click(x, y, *args):

In this case, if there will be more than 2 values in list, no problem, 3 to onwards will be in args tuple.

As you say that the order of the coordinates is inverted, we can use two auxiliary variables to unpack the values and then pass them in the required order:

y, x = found_pixels_coords
click(x, y)

If found_pixels_coords is a list with several coordinates, then you can pick the first one with:

y, x = found_pixels_coords[0]
click(x, y)
Related