PyAutoGui图像操作(二):图像定位不稳定解决方案
| 分类: pythonlua |
转:https://www.iotword.com/3849.html
一,PyAutoGui介绍
二,PyAutoGui安装
cmd安装命令:
pip install PyAutoGUI
如果因为python或者pip版本安装失败,以下提供一种解决办法:
三,图片定位不稳定的四种解决方案
1,方案介绍
confidence参数,也就是识别准确度,当confidence越小时,定位的精度就会越低,从而实现模糊定位。
2,模糊查询
pip install opencv-python
pyautogui.locateOnScreen()函数中加入confidence参数,当confidence的值决定精度
>>>t = locateOnScreen('images/test1.png', confidence=0.9)
>>>print(center(t))
Point(x=873, y=1056)
3,灰度匹配
pyautogui.locateOnScreen()函数中加入grayscale=True,就可以实现灰度匹配
>>>t = locateOnScreen('images/test1.png', grayscale=True)
>>>print(center(t))
Point(x=873, y=1056)
4,指定范围:
>>>t = locateOnScreen('images/test1.png', region=(0,940,1919,939))
>>>print(center(t))
Point(x=873, y=1056)
5,多图定位
from pyautogui import *
#把字符串按'|'切割
def word_cut(args):
tup = []
if '|' in args:
re1 = args.split('|')
return re1
else:
tup.append(args)
return tuple(tup)
#判断图像是否找到,如果找到就返回True,没找到就跳过
def assertPIC(args):
if locateOnScreen(args) == None:
pass
else:
return True
#循环找图,找到就返回图像中心点,没找到就打印'没找到'
def img_locat(args):
arg = word_cut(args)
for i in range(len(arg)):
if assertPIC(arg[i]):
return center(locateOnScreen(arg[i]))
else:
print('没找到')
#测试
print(img_locat('images/test.png|images/test1.png'))
结果为:
https://i3.wp.com/img-blog.csdnimg.cn/2d1ae052af7c43bebfd077dd322655d3.png

加载中…