Python自动化操作

Python自动化操作——PyAutoGUI

在桌面实现自动化操作,与浏览器页面自动化操作类似,桌面自动化操作也是需要定位鼠标在桌面的位置,然后根据定位的位置执行对应的操作

先安装

pip3 install pyautogui

鼠标操作

鼠标移动

整个桌面是以左上角为坐标轴的原点,所有的操作都以这个原点,来确定操作位置

pyautogui.moveTo(200,400,duration=2)
pyautogui.moveRel(200,500,duration=2)

第一行是将鼠标移动到指定的像素(200,400)位置,第二行代码是将鼠标按照当前点向右移动200px,向下移动400px这个方向移动

参数 duration 表示移动时间,即在指定时间内完成移动操作,单位是秒

获取鼠标坐标位置

print(pyautogui.position())

鼠标点击

# 鼠标点击,默认左键
pyautogui.click(100,100)
# 单击左键
pyautogui.click(100,100,button='left')
# 单击右键
pyautogui.click(100,300,button='right')
# 单击中间
pyautogui.click(100,300,button='middle')

鼠标点击,如果不指定 button 参数,默认是点击左键,前面两个参数就是点击坐标的位置

# 双击左键
pyautogui.doubleClick(10,10)
# 双击右键
pyautogui.rightClick(10,10)
# 双击中键
pyautogui.middleClick(10,10)

# 鼠标按下
pyautogui.mouseDown()
# 鼠标释放
pyautogui.mouseUp()

屏幕处理

获取屏幕截图

 

1 screenshot()函数
screenshot()函数会返回Image对象,也可以设置文件名

import pyautogui

im1 = pyautogui.screenshot()
im2 = pyautogui.screenshot('my_screenshot.png')
在一个 1920×1080 的屏幕上,screenshot()函数要消耗100微秒 ——不快也不慢。

如果你不需要截取整个屏幕,还有一个可选的region参数。你可以把截取区域的左上角XY坐标值和宽度、高度传入截取。

img_path = r'C:\Users\Administrator\Desktop\one.png'
# 截图
im = pyautogui.screenshot(region=(0, 0, 300 ,400))
# 保存图片
im.save('1.png')

2 locateOnScreen()和center()函数
locateOnScreen()函数来获得图片坐标,返回元组

注:未成功识别,返回None;成功识别,返回首次发现该图像时左边的x,y坐标、宽度和高度。如果该图像在屏幕上能够找到多处,locateAllOnScreen()函数返回一个list

这个元组可以用pyautogui.center()函数来获取截图屏幕的中心坐标。

代码如下:

In [1]: import pyautogui

In [2]: # 图片保存路径
...: img_path = r'C:\Users\Administrator\Desktop\one.png'

In [3]: # 识别图片, 未成功识别,返回None; 成功识别,返回首次发现该图像时左边的x,y坐标,宽度和高度
...: pos = pyautogui.locateOnScreen(img_path)

In [4]: pos
Out[4]: Box(left=0, top=0, width=30, height=30)

In [5]: # 返回该区域中心的x,y坐标
...: pyautogui.center(pos)
Out[5]: Point(x=15, y=15)

# 图像识别(一个)
oneLocation = pyautogui.locateOnScreen('1.png')
print(oneLocation) 

# 图像识别(多个)
allLocation = pyautogui.locateAllOnScreen('1.png')
print(list(allLocation))

 

# 精确度设为0.9,不然默认是1,太苛刻

pyautogui.locateCenterOnScreen(img, confidence=0.9)


3 灰度值匹配
locateOnScreen()函数可以把grayscale参数设置为True来加速定位(大约提升30%),默认为False。这种去色(desaturate)方法可以加速定位,但是也可能导致假阳性(false-positive)匹配。

In [6]: pyautogui.locateOnScreen(img_path, grayscale=True)
Out[6]: Box(left=0, top=0, width=30, height=30)

4 像素匹配
要获取截屏某个位置的RGB像素值,可以用Image对象的getpixel()方法:

In [1]: import pyautogui

In [2]: im = pyautogui.screenshot()

In [3]: im.getpixel((100, 200))
Out[3]: (255, 255, 255)

也可以用PyAutoGUI的pixel()函数,是之前调用的包装:

In [4]: pyautogui.pixel(100, 200)
Out[4]: (255, 255, 255)

如果你只是要检验一下指定位置的像素值,可以用pixelMatchesColor()函数,把X、Y和RGB元组值传入即可:

In [5]: pyautogui.pixelMatchesColor(100, 200, (255, 255, 255))
Out[5]: True

In [6]: pyautogui.pixelMatchesColor(100, 200, (255, 255, 245))
Out[6]: False

tolerance参数可以指定红、绿、蓝3种颜色误差范围:

In [7]: pyautogui.pixelMatchesColor(100, 200, (255, 255, 245), tolerance=10)
Out[7]: True

In [8]: pyautogui.pixelMatchesColor(100, 200, (248, 250, 245), tolerance=10)
Out[8]: True

In [9]: pyautogui.pixelMatchesColor(100, 200, (205, 255, 245), tolerance=10)
Out[9]: False

键盘输入

键盘函数

  • keyDown():模拟按键按下
  • keyUP():模拟按键松开
  • press():模拟一次按键过程,即 keyDown 和 keyUP 的组合
  • typewrite():模拟键盘输出内容

# 下面代码可以得到一个!

pyautogui.keyDown('shift')
pyautogui.press('1')
pyautogui.keyUp('shift')

# 可以直接输出内容

pyautogui.typewrite('python', 1)

特殊符号

# 运行下面代码,编辑器里面就会输出 python 之后换行

pyautogui.typewrite(['p','y','t','h','o','n','enter'])

快捷键

# 执行 ctrl + c 命令

pyautogui.hotkey('ctrl','c')

信息框

# 弹出一个选择框来中断当前的操作,选择操作分支

way = pyautogui.confirm('领导,该走哪条路?', buttons=['农村路', '水路', '陆路'])
print(way)

# 警告框
alert = pyautogui.alert(text='警告!敌军来袭!', title='警告框')
print(alert)
# 密码框
password = pyautogui.password('请输入密码')
print(password)
# 普通输入框
input = pyautogui.prompt('请输入指令:')
print(input)

 

posted @ 2022-07-23 21:33  听风恋故人  阅读(1880)  评论(0)    收藏  举报