python简易自动化之pyautogui 2020-12-19
学习自动化,参考网上介绍,测试了一下,效果很好。记录如下。
- pip install pyautogui 安装pyautogui模块,这是图片识别模块。更多帮助请参阅: https://github.com/asweigart/pyautogui/pyautogui帮助文档
- 使用python进行编程,基本原理是自动识别软件界面,然后根据截取的图片进行自动识别坐标位置,传入鼠标信息或者键盘信息。
- 代码如下:
import pyautogui
import time
#import cv2 #opencv-python
'''
1.需要点击得图片到当前目录下。确保图片颜色背景等都相同.最好设置为黑色背景!!!!!!!!!!
2. 计算器切换到标准模式下,因为所有图片都是在标准模式下得到得。
3. 运行程序。
4.需要退出时,切换屏幕到另一个截面并保持,选择设置control文件为end=1,保存后在
#循环结束时自动退出。
'''
time.sleep(1)
# Windows计算器的按钮截图
path='.\\calc\\'
five = path+'5.png'
eight = path+'8.png'
eight_program=path+'8p.png'
four_program=path+'4p.png'
two_program=path+'2p.png'
one_program=path+'1p.png'
zero_program=path+'0p.png'
multiply = path+'multiply.png'
equals = path+'equals.png'
menu = path+'menu.png'
biaozhun = path+'biaozhun.png'
kexue = path+'kexue.png'
kexuekexue=path+'kexuekexue.png'
chengxuyuan=path+'chengxuyuan.png'
hexH=path+'hex.png'
binB=path+'bin.png'
dec=path+'dec.png'
blank=path+'blank.png'
#backspace
bs = path+'bs.png'
ce = path+'ce.png'#ce
# 图片识别和点击的函数
confidenceValue=0.9
def find_and_click(image):
#print(pyautogui.locateCenterOnScreen(image, confidence=confidenceValue))
x=0;
y=0
try:
x,y= pyautogui.locateCenterOnScreen(image, confidence=0.8)
except Exception as e:
print(e)
return 1;
if(x>0 and y>0):
ss="x={0}\ty={1}".format(x,y,x)
print(ss)
elif(x=='None' or y=='None'):
print(x)
return 1;
#传入单击动作
pyautogui.click(x, y)
return ss
try:#移动到空白位置
x,y= pyautogui.locateCenterOnScreen(blank, confidence=0.75)
except Exception as e:
print(e)
return 1;
def doCalc():
delay=0.1
find_and_click(menu)
time.sleep(delay)
find_and_click(biaozhun)
time.sleep(delay)
# 执行5*8=
five1=path+'6.png'
#find_and_click(five1)
find_and_click(ce)
find_and_click(five)
find_and_click(multiply)
find_and_click(eight)
#pyautogui.click(path+'8.png') #直接判断图片单击
find_and_click(multiply)
#pyautogui.click(path+'8.png') #直接判断图片单击
find_and_click(eight)
find_and_click(multiply)
find_and_click(eight)
find_and_click(multiply)
find_and_click(five)
#pyautogui.click(path+'5.png') #直接判断图片单击
find_and_click(multiply)
find_and_click(eight)
find_and_click(equals)
print("菜单测试")
find_and_click(menu)
time.sleep(delay)
find_and_click(biaozhun)
time.sleep(delay)
find_and_click(menu)
time.sleep(delay)
find_and_click(kexue)#科学
time.sleep(delay)
print('科学》程序员')
find_and_click(menu)
time.sleep(delay)
find_and_click(chengxuyuan) #程序员
time.sleep(delay)
find_and_click(eight_program)
find_and_click(four_program)
find_and_click(two_program)
find_and_click(one_program)
#find_and_click(zero_program)
time.sleep(delay)
find_and_click(hexH)
time.sleep(delay)
find_and_click(dec)
find_and_click(one_program)
find_and_click(two_program)
time.sleep(delay)
find_and_click(eight_program)
time.sleep(delay)
find_and_click(hexH)
time.sleep(delay)
find_and_click(binB)
find_and_click(dec)
print("测试88888")
#find_and_click(path+"delete.png") #直接判断图片单击
find_and_click(menu)
time.sleep(delay)
find_and_click(biaozhun)
time.sleep(delay)
find_and_click(eight)
find_and_click(eight)
find_and_click(eight)
find_and_click(eight)
find_and_click(eight)
print("退格键")
time.sleep(delay)
find_and_click(bs)
find_and_click(bs)
'''
'''
def test():
delay=0.1
print("菜单测试")
find_and_click(menu)
print('科学》程序员')
find_and_click(kexue)
time.sleep(delay)
find_and_click(menu)
find_and_click(chengxuyuan) #程序员
time.sleep(delay)
find_and_click(eight_program)
time.sleep(delay)
find_and_click(hexH)
time.sleep(delay)
find_and_click(eight)
time.sleep(delay)
time.sleep(delay)
find_and_click(hexH)
time.sleep(delay)
find_and_click(binB)
find_and_click(dec)
print("清除计算历史")
find_and_click(path+"delete.png") #直接判断图片单击
if __name__ == "__main__":
i=0;j=0
loopNumber=100
while(j<10 and i<loopNumber):
doCalc();
#test()
ss="第{0}次循环。i={1}\tj={2}".format(i+1,i,j)
print(ss)
i+=1
if(i==loopNumber):
i=0
j+=1
with open('./calc/control.txt') as control:
ctl=control.readline().strip()
ss="是否结束自动计算循环?=1则结束。 end={}".format(ctl)
print(ss)
if(ctl=='end=1'):
break
计算器截图后截取需要的按键图片,保存在相对路径./calc/下:
以上重复的图片,是在计算器的标准模式和科学/程序员模式下分别截图,各自是有很大区别的。务必注意。
5. 详细说明参见代码中内容。
6. `运行中的效果图片:
- 可是使用其它程序,修改control文件的end=0为end=1来结束程序(一个循环结束后自动结束)
- 扩展应用:对于简单重复的工作,可以使用这个办法自动执行,从而解放人类。
- 是不是说,人类的进步,都是懒人推动的?因为只有他们才会想出需要使用便捷的方法去解决问题。