匹配获取屏幕中多个相同图像坐标

前几天发现各家RPA都没有对屏幕区域中存在的多个符合相似度要求的图像进行判断的功能,我用python简单实现了一下,贴到这里分享给大家。
代码部分:
============================================
import pyautogui
def main(template_path, confidence, region):
# 自动修正region格式
if region and len(region) == 4:
# 如果后两个值大于屏幕尺寸,则视为坐标进行转换
screen_width, screen_height = pyautogui.size()
if region[2] > screen_width or region[3] > screen_height:
region = (region[0], region[1],
region[2] - region[0],
region[3] - region[1])
try:
instances = list(pyautogui.locateAllOnScreen(
template_path,
confidence=confidence,
region=region,
grayscale=False
))
except Exception as e:
print(f"发生错误: {str(e)}")
return []
print(f"找到 {len(instances)} 个实例,坐标如下:{instances}")
for idx, loc in enumerate(instances):
print(f"实例 {idx+1}: 左上角坐标 {loc}, 宽高 {loc[2]}x{loc[3]}")
return [[loc.left, loc.top] for loc in instances]
if __name__ == "__main__":
messages = main(
template_path = "e://1.bmp",
confidence = 0.8,
region = None
)
print(messages)
============================================
简单说明:
在python模块中使用代码,新建一个可视化模块对python模块进行调用。

传参有3个:
template_path:你要查找的图像路径,名称不能有汉字;
confidence:匹配度,0.1-1之间,建议0.8;
region:搜索区域,(1892, 340, 2211, 604) ——(左,上,宽,高),None表示全屏,后两个值也可以传入坐标,会自动转换。
grayscale=False,灰度处理。如果你的匹配图形色彩干扰低,形状区分度高或者有明显的可OCR识别部分,可以设置为True,可以大幅提示识别速度。
返回我只取了相对屏幕左、上坐标作为返回,完整的返回包含了左,上,宽,高,可以自己修改。
3人点赞
后可进行评论
扫码关注
获取专业的解决方案
帮您实现业务爆发式的增长




