pyautogui屏幕识图获取坐标点
一、安装pyautogui
命令提示符输入 pip install PyAutoGUI 等待安装完毕即可。
二、代码如下
import pyautogui as pag
import time
#img1=pag.screenshot('1.png') # 屏幕截图并保存为1.png
location1=None
try:
location1=pag.locateCenterOnScreen('building.png',grayscale=True,confidence=0.7)#获取图片位置中心点坐标
location1_x,location1_y=location1 #将中心点坐标分别赋值给location1_x,location1_y
pag.click(location1_x,location1_y)#模拟鼠标点击
time.sleep(5)#等待5秒
attacklocation=pag.locateCenterOnScreen('attack.png',grayscale=True,confidence=0.9)
attack_x,attack_y=attacklocation
pag.click(attack_x,attack_y)
time.sleep(5)
attacklocation1=pag.locateCenterOnScreen('attack1.png',grayscale=True,confidence=0.7)
attack_x1,attack_y1=attacklocation1
pag.click(attack_x1,attack_y1)
except pag.ImageNotFoundException:
print("not found")
总结
先将要识别的图像截图保存下来记为1.png或者其他格式图片,然后放到pag.locateCenterOnScreen('1.png',grayscale=True,confidence=0.9)
灰度我是给True,置信度低一点可以更好找到要识别的图片。
得到坐标后,就可以模拟鼠标按键点击