python pyautogui實現(xiàn)圖片識別點擊失敗后重試功能
安裝庫
pip install Pillow pip install opencv-python
confidence作用
confidence 參數(shù)是用于指定圖像匹配的信度(或置信度)的,它表示圖像匹配的準確程度。這個參數(shù)的值在 0 到 1 之間,數(shù)值越高表示匹配的要求越嚴格。
具體來說,confidence 參數(shù)用于調(diào)整在屏幕上搜索目標圖像時的匹配精度:
0.0 表示完全不匹配。
1.0 表示完全匹配。
在實際應用中,圖像匹配的信度可以幫助你處理一些圖像上的細微差異。例如,屏幕上的圖像可能因為分辨率、光線、顏色等原因與原始圖像有些不同。通過調(diào)整 confidence 參數(shù),你可以設置一個合理的閾值,使得圖像匹配過程既不太嚴格(導致找不到圖像),也不太寬松(導致誤匹配)。
舉個例子,如果你設置 confidence=0.8,那么只有當屏幕上的圖像與目標圖像的相似度達到 80% 以上時,才會被認為是匹配的。
識別圖片點擊
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"達到最大重試次數(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ù)設置為 None
location = locate_and_click_image(image_path, retry_interval, max_retries, click_count, confidence)
if location:
print("操作完成。")
else:
print("未能定位到圖片,程序結束。")
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"達到最大重試次數(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']},程序結束。")
if __name__ == "__main__":
main()優(yōu)化代碼,識別多張圖片,只要識別到圖片就結束循環(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"達到最大重試次數(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()到此這篇關于python pyautogui實現(xiàn)圖片識別點擊失敗后重試的文章就介紹到這了,更多相關python pyautogui圖片識別失敗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Python pyautogui模擬鍵盤輸入操作的示例詳解
- python pyautogui手動活動(模擬鼠標鍵盤)自動化庫使用
- Python利用PyAutoGUI輕松搞定圖片上傳
- Python自動操作神器PyAutoGUI的使用教程
- Python利用PyAutoGUI模塊實現(xiàn)控制鼠標鍵盤
- Python中PyAutoGUI幫助文檔(推薦!)
- python教程之利用pyautogui圖形自動化擊敗重復性辦公任務
- Python中pyautogui庫的使用方法匯總
- python編程PyAutoGUI庫使用與安裝簡介
- python 利用PyAutoGUI快速構建自動化操作腳本
- Python中PyAutoGUI?庫的使用
相關文章
python 找出list中最大或者最小幾個數(shù)的索引方法
今天小編就為大家分享一篇python 找出list中最大或者最小幾個數(shù)的索引方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
python使用socket實現(xiàn)的傳輸demo示例【基于TCP協(xié)議】
這篇文章主要介紹了python使用socket實現(xiàn)的傳輸demo,結合實例形式分析了Python使用socket庫基于TCP協(xié)議實現(xiàn)的客戶端與服務器端相關操作技巧,需要的朋友可以參考下2019-09-09

