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

