欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python pyautogui實現(xiàn)圖片識別點擊失敗后重試功能

 更新時間:2024年06月26日 15:23:02   作者:U盤失蹤了  
這篇文章主要介紹了python pyautogui實現(xiàn)圖片識別點擊失敗后重試效果,本文通過實例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧

安裝庫 

pip install Pillow
pip install opencv-python

confidence作用

confidence 參數(shù)是用于指定圖像匹配的信度(或置信度)的,它表示圖像匹配的準(zhǔn)確程度。這個參數(shù)的值在 0 到 1 之間,數(shù)值越高表示匹配的要求越嚴(yán)格。
具體來說,confidence 參數(shù)用于調(diào)整在屏幕上搜索目標(biāo)圖像時的匹配精度:
0.0 表示完全不匹配。
1.0 表示完全匹配。
在實際應(yīng)用中,圖像匹配的信度可以幫助你處理一些圖像上的細(xì)微差異。例如,屏幕上的圖像可能因為分辨率、光線、顏色等原因與原始圖像有些不同。通過調(diào)整 confidence 參數(shù),你可以設(shè)置一個合理的閾值,使得圖像匹配過程既不太嚴(yán)格(導(dǎo)致找不到圖像),也不太寬松(導(dǎo)致誤匹配)。
舉個例子,如果你設(shè)置 confidence=0.8,那么只有當(dāng)屏幕上的圖像與目標(biāo)圖像的相似度達(dá)到 80% 以上時,才會被認(rèn)為是匹配的。

識別圖片點擊

import pyautogui
import time
import os
def locate_and_click_image(image_path, retry_interval=2, max_retries=5, click_count=1, confidence=None):
    """
    定位圖片并點擊指定次數(shù)。
    :param image_path: 圖片路徑
    :param retry_interval: 重試間隔時間(秒)
    :param max_retries: 最大重試次數(shù)
    :param click_count: 點擊次數(shù)
    :param confidence: 圖像匹配的信度(0到1之間),需要安裝 OpenCV
    :return: 圖片的位置 (x, y, width, height) 或 None(如果未找到)
    """
    if not os.path.isfile(image_path):
        print(f"錯誤:圖片路徑無效或文件不存在: {image_path}")
        return None
    retries = 0
    while retries < max_retries:
        try:
            if confidence is not None:
                location = pyautogui.locateOnScreen(image_path, confidence=confidence)
            else:
                location = pyautogui.locateOnScreen(image_path)
            if location is not None:
                print(f"找到圖片: {image_path},位置: {location}")
                center = pyautogui.center(location)
                for _ in range(click_count):
                    pyautogui.click(center)
                    print(f"點擊圖片中心位置。點擊次數(shù):{_ + 1}")
                return location
            else:
                print(f"未找到圖片: {image_path},{retry_interval}秒后重試...(重試次數(shù): {retries + 1}/{max_retries})")
                time.sleep(retry_interval)
                retries += 1
        except pyautogui.ImageNotFoundException:
            print(f"未找到圖片: {image_path},{retry_interval}秒后重試...(重試次數(shù): {retries + 1}/{max_retries})")
            time.sleep(retry_interval)
            retries += 1
    print(f"達(dá)到最大重試次數(shù): {max_retries},未找到圖片: {image_path}")
    return None
def main():
    image_path = '1.png'  # 替換為你的圖片路徑
    retry_interval = 2
    max_retries = 5
    click_count = 1
    confidence = 0.8  # 如果不使用 OpenCV,請將此參數(shù)設(shè)置為 None
    location = locate_and_click_image(image_path, retry_interval, max_retries, click_count, confidence)
    if location:
        print("操作完成。")
    else:
        print("未能定位到圖片,程序結(jié)束。")
if __name__ == "__main__":
    locate_and_click_image('1.png', retry_interval=2, max_retries=5, click_count=2, confidence=0.8)
 

優(yōu)化代碼,識別多張圖片并點擊

import pyautogui
import time
import os
def locate_and_click_image(path, retry_interval=2, max_retries=5, click_count=1, confidence=None):
    if not os.path.isfile(path):
        print(f"錯誤:圖片路徑無效或文件不存在: {path}")
        return None
    retries = 0
    while retries < max_retries:
        try:
            if confidence is not None:
                location = pyautogui.locateOnScreen(path, confidence=confidence)
            else:
                location = pyautogui.locateOnScreen(path)
            if location is not None:
                print(f"找到圖片: {path},位置: {location}")
                center = pyautogui.center(location)
                for _ in range(click_count):
                    pyautogui.click(center)
                    print(f"點擊圖片中心位置。點擊次數(shù):{_ + 1}")
                return location
            else:
                print(f"未找到圖片: {path},{retry_interval}秒后重試...(重試次數(shù): {retries + 1}/{max_retries})")
                time.sleep(retry_interval)
                retries += 1
        except pyautogui.ImageNotFoundException:
            print(f"未找到圖片: {path},{retry_interval}秒后重試...(重試次數(shù): {retries + 1}/{max_retries})")
            time.sleep(retry_interval)
            retries += 1
    print(f"達(dá)到最大重試次數(shù): {max_retries},未找到圖片: {path}")
    return None
def main():
    images = [
        {'path': '1.png', 'retry_interval': 2, 'max_retries': 5, 'click_count': 1, 'confidence': 0.8},
        {'path': '3.png', 'retry_interval': 2, 'max_retries': 5, 'click_count': 1, 'confidence': 0.8},
        # 添加更多圖片
    ]
    for image in images:
        location = locate_and_click_image(**image)
        if location:
            print(f"圖片 {image['path']} 操作完成。")
        else:
            print(f"未能定位到圖片 {image['path']},程序結(jié)束。")
if __name__ == "__main__":
    main()

優(yōu)化代碼,識別多張圖片,只要識別到圖片就結(jié)束循環(huán)

import pyautogui
import time
import os
def locate_and_click_image(path, retry_interval=2, max_retries=5, click_count=1, confidence=None):
    if not os.path.isfile(path):
        print(f"錯誤:圖片路徑無效或文件不存在: {path}")
        return None
    retries = 0
    while retries < max_retries:
        try:
            if confidence is not None:
                location = pyautogui.locateOnScreen(path, confidence=confidence)
            else:
                location = pyautogui.locateOnScreen(path)
            if location is not None:
                print(f"找到圖片: {path},位置: {location}")
                center = pyautogui.center(location)
                for _ in range(click_count):
                    pyautogui.click(center)
                    print(f"點擊圖片中心位置。點擊次數(shù):{_ + 1}")
                return True
            else:
                print(f"未找到圖片: {path},{retry_interval}秒后重試...(重試次數(shù): {retries + 1}/{max_retries})")
                time.sleep(retry_interval)
                retries += 1
        except pyautogui.ImageNotFoundException:
            print(f"未找到圖片: {path},{retry_interval}秒后重試...(重試次數(shù): {retries + 1}/{max_retries})")
            time.sleep(retry_interval)
            retries += 1
    print(f"達(dá)到最大重試次數(shù): {max_retries},未找到圖片: {path}")
    return False
def main():
    images = [
        {'path': '1.png', 'retry_interval': 2, 'max_retries': 5, 'click_count': 1, 'confidence': 0.8},
        {'path': '3.png', 'retry_interval': 2, 'max_retries': 5, 'click_count': 1, 'confidence': 0.8},
        {'path': '4.png', 'retry_interval': 2, 'max_retries': 5, 'click_count': 1, 'confidence': 0.8},
        # 添加更多圖片
    ]
    for image in images:
        success = locate_and_click_image(**image)
        if success:
            print(f"圖片 {image['path']} 操作完成。")
            break
        else:
            print(f"未能定位到圖片 {image['path']}。")
if __name__ == "__main__":
    main()

到此這篇關(guān)于python pyautogui實現(xiàn)圖片識別點擊失敗后重試的文章就介紹到這了,更多相關(guān)python pyautogui圖片識別失敗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python檢驗用戶輸入密碼的復(fù)雜度

    Python檢驗用戶輸入密碼的復(fù)雜度

    這篇文章主要介紹了Python檢驗用戶輸入密碼的復(fù)雜度,在用戶設(shè)置密碼的時候檢測輸入的密碼大小寫數(shù)字等,需要的朋友可以參考下
    2023-04-04
  • 簡單談?wù)刾ython基本數(shù)據(jù)類型

    簡單談?wù)刾ython基本數(shù)據(jù)類型

    在Python中,能夠直接處理的數(shù)據(jù)類型有以下幾種:#整型 int,#浮點型 float,#布爾型 bool,#復(fù)數(shù)型 (在python中用小寫 j ,表示虛部,用其他的字母不行)complex
    2018-09-09
  • python中bytes和str類型的區(qū)別

    python中bytes和str類型的區(qū)別

    這篇文章主要介紹了python中bytes和str類型的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • python 找出list中最大或者最小幾個數(shù)的索引方法

    python 找出list中最大或者最小幾個數(shù)的索引方法

    今天小編就為大家分享一篇python 找出list中最大或者最小幾個數(shù)的索引方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • python如何在字典中插入或增加一個字典

    python如何在字典中插入或增加一個字典

    這篇文章主要介紹了python如何在字典中插入或增加一個字典問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • python使用socket實現(xiàn)的傳輸demo示例【基于TCP協(xié)議】

    python使用socket實現(xiàn)的傳輸demo示例【基于TCP協(xié)議】

    這篇文章主要介紹了python使用socket實現(xiàn)的傳輸demo,結(jié)合實例形式分析了Python使用socket庫基于TCP協(xié)議實現(xiàn)的客戶端與服務(wù)器端相關(guān)操作技巧,需要的朋友可以參考下
    2019-09-09
  • Python網(wǎng)絡(luò)爬蟲之爬取微博熱搜

    Python網(wǎng)絡(luò)爬蟲之爬取微博熱搜

    這篇文章主要介紹了Python網(wǎng)絡(luò)爬蟲之爬取微博熱搜的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-04-04
  • python?字典的概念敘述和使用方法

    python?字典的概念敘述和使用方法

    Python中還有一個很重要的數(shù)據(jù)類型就是字典,其實集合的底層使用的也是字典,這篇文章主要介紹了python?字典的概念敘述和使用方法,需要的朋友可以參考下
    2023-02-02
  • 自定義PyCharm快捷鍵的設(shè)置方式

    自定義PyCharm快捷鍵的設(shè)置方式

    這篇文章主要介紹了自定義PyCharm快捷鍵的設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • python如何運行js語句

    python如何運行js語句

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于python如何運行js語句的相關(guān)內(nèi)容,有興趣的朋友們可以參考下。
    2020-09-09

最新評論