ищите изображение по caputura экрана, даже если оно не в этот момент - PullRequest
0 голосов
/ 24 мая 2019

Какой код можно использовать, если я хочу посмотреть изображение, даже если оно не в тот момент, а когда оно найдено?Мой код будет:

import pyautogui as auto
import time
import pyautogui


def Saludar (seconds,schedule) :

    while (1>0) and (schedule == True) :
        time.sleep(seconds)

        x, y = auto.locateCenterOnScreen('linea.png', grayscale=True)
        auto.moveTo(x,y)
        pyautogui.click()
        print("YA")



if __name__== "__main__":
    Saludar(2,True)

Ошибка:

Traceback (most recent call last):
  File "C:/Users/mario/Desktop/buscar aun si no está.py", line 19, in <module>
    Saludar(2,True)
  File "C:/Users/mario/Desktop/buscar aun si no está.py", line 11, in Saludar
    x, y = auto.locateCenterOnScreen('linea.png', grayscale=True)
TypeError: 'NoneType' object is not iterable

1 Ответ

0 голосов
/ 27 мая 2019

Если изображение не найдено, оно вызывает исключение TypeError. Вы можете просто справиться с этим с помощью try / кроме :

import pyautogui as auto
import time
import pyautogui


def Saludar(seconds, schedule):
    while (1 > 0) and (schedule == True):
        time.sleep(seconds)
        try:
            x, y = auto.locateCenterOnScreen(
                'Desktop/linea.png', grayscale=True)
            print('Found it!')
            auto.moveTo(x, y)
            pyautogui.click()
        except TypeError:
            """
            Image is not found
            """
            print("Image is Not found on the screen!")


if __name__ == "__main__":
    Saludar(2, True)
...