This commit is contained in:
HuangHai
2026-01-14 07:28:06 +08:00
parent 4cc6bc06d1
commit af60249fce
19 changed files with 166 additions and 28 deletions

View File

@@ -151,7 +151,7 @@ def click_image_template(d, template_path, timeout=5.0, threshold=0.8):
max_val, max_loc, r_w, r_h = best_match
center_x = max_loc[0] + r_w // 2
center_y = max_loc[1] + r_h // 2
logger.info(f"Found template at ({center_x}, {center_y}) with confidence {max_val:.2f}")
logger.info(f"成功点击图片模板: {template_path}, 匹配度: {max_val:.2f}")
d.click(center_x, center_y)
return True
@@ -160,6 +160,38 @@ def click_image_template(d, template_path, timeout=5.0, threshold=0.8):
return False
def setup_logger(name, log_file=None):
"""
配置日志,支持同时输出到控制台和文件
"""
if log_file is None:
log_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Logs")
if not os.path.exists(log_dir):
os.makedirs(log_dir)
log_file = os.path.join(log_dir, f"{name}_{time.strftime('%Y%m%d')}.log")
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
# 如果已经有 handler,不再添加
if logger.handlers:
return logger
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# 控制台 Handler
ch = logging.StreamHandler()
ch.setFormatter(formatter)
logger.addHandler(ch)
# 文件 Handler
fh = logging.FileHandler(log_file, encoding='utf-8')
fh.setFormatter(formatter)
logger.addHandler(fh)
return logger
def find_template_coords(img_path, template_path, threshold=0.8):
"""
在图片中查找模板并返回中心坐标