[Python] Locate Image on Screen with pyautogui

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to locate a particular image on a screen. The image is stored in a directory as CL9. I've tried multiple ways of setting the image_path, but none of them seem to work. Ultimately, I'll want to loop through up to 100 images in the same directory, but I wanted to code a little, test a little, code a little more, test a little more, etc.

    Here's what I have:
    # Locate and click on a particular image on the screen.
    #image_path = ('C:/Users/reber/OneDrive/Desktop/Python/Rise of Castles/Images/CL9.png')
    #image_path = r('C:/Users/reber/OneDrive/Desktop/Python/Rise of Castles/Images/CL9.png')
    image_path = (r'C:/Users/reber/OneDrive/Desktop/Python/Rise of Castles/Images/CL9.png')
    #image_path = "/images/CL9.png"
    location = pyautogui.locateOnScreen('image_path')
    pyautogui.click(location)


    Here's what I'm getting:

    c:\Users\reber\.vscode\extensions\ms-python.debugpy-2024.10.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '56706' '--' 'c:\Users\reber\OneDrive\Desktop\Python\Rise of Castles\.venv\GetAllianceDetails.py'
    Traceback (most recent call last):
    File "c:\Users\reber\OneDrive\Desktop\Python\Rise of Castles\.venv\GetAllianceDetails.py", line 15, in <module>
    location = pyautogui.locateOnScreen('image_path')
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "c:\Users\reber\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyautogui\__init__.py", line 172, in wrapper
    return wrappedFunction(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "c:\Users\reber\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyautogui\__init__.py", line 210, in locateOnScreen
    return pyscreeze.locateOnScreen(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "c:\Users\reber\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyscreeze\__init__.py", line 405, in locateOnScreen
    retVal = locate(image, screenshotIm, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "c:\Users\reber\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyscreeze\__init__.py", line 383, in locate
    points = tuple(locateAll(needleImage, haystackImage, **kwargs))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "c:\Users\reber\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyscreeze\__init__.py", line 282, in _locateAll_pillow
    needleFileObj = open(needleImage, 'rb')
    ^^^^^^^^^^^^^^^^^^^^^^^
    FileNotFoundError: [Errno 2] No such file or directory: 'image_path'

    Continue reading...

Compartilhe esta Página