找图
from ascript.android.screen import FindImages
从屏幕中找到 局部图片 的 位置,支持透明 PNG、多图搜索、多尺度匹配。
使用场景
- 在屏��上查找特定图标、按钮、头像等图片元素的位置
- 判断某个 UI 元素是否出现(如加载完成的图标、红点提醒)
- 游戏中查找怪物、道具、技能图标等固定图案
- 一次查找多张模板图,判断当前匹配的是哪一个
- 跨分辨率设备适配,同一套模板图在不同 DPI 下使用
返回值格式
所有找图方法返回 dict 字典(单个)或 dict 数组(多个):
{
'rect': [915, 1761, 1076, 1899], # 子图在屏幕中的范围
'center_x': 995, # 中心点 X 坐标
'center_y': 1830, # 中心点 Y 坐标
'confidence': 0.949, # 相似度 0-1
'index': 0 # 多图搜索时,属于第几张模板图
}
方法
find — 找一个
在屏幕中查找局部图片,返回第一个匹配结果。
- 函数
FindImages.find(part_img, rect=None, confidence=0.1, rgb=False,
source_img=None, ori=2, scale_range=None, image=None)
- 参数
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| part_img | str/list | 是 | 局部图片路径或路径列表。只填文件名时自动在 res/img/ 下查找 |
| rect | list | 否 | 搜索范围 [x, y, x1, y1] |
| confidence | float | 否 | 可信度阈值 0-1,低于此值的结果被过滤,默认 0.1 |
| rgb | bool | 否 | True=彩色匹配,False=灰度匹配(可找到相同形状不同颜色的图案) |
| ori | int | 否 | 排序方向 1-8,默认 2(左上→右下横向) |
| scale_range | list | 否 | 多尺度匹配范围,如 [0.5, 2.0],None=单尺度 |
| image | Bitmap/ndarray | 否 | 传入源图,None=自动截屏 |
- 示例
from ascript.android.screen import FindImages
from ascript.android.system import R
# 基本找图
res = FindImages.find(R.res("/img/btn.png"))
if res:
print("位置:", res["center_x"], res["center_y"])
print("相似度:", res["confidence"])
# 指定范围 + 可信度
res = FindImages.find(R.res("/img/btn.png"), rect=[59, 932, 374, 1207], confidence=0.8)
# 一次找多张图
res = FindImages.find(R.img("a.png", "b.png", "c.png"), confidence=0.8)
if res:
print("匹配的是第", res["index"], "张图")