autopy使用

很简单的一个库

安装

与常规python库类似

pip install autopy 
# 或:
pip install -U autopy

基础使用

官方tutorial
需要注意的是, 核心代码应该不是python代码(是.pyd), 所以在IDE中并不会提示参数, 使用时需要经常参照文档

autopy.alert

一个autopy用例:最简单的定时提醒工具

import autopy as at
import datetime
import time
while(True):
    now = datetime.datetime.now()
    fmt = '%y-%m-%d, %H:%M:%S'
    if datetime.datetime(2019,11,7,19,46)<now:
        at.alert.alert('now is '+ now.strftime(fmt))
        break    
    time.sleep(1) 

鼠标键盘操控

鼠标相关

import autopy, math
TWO_PI = 3.1416*2
autopy.mouse.move(x=1, y=1)
autopy.mouse.smooth_move(1, 1, 2)#最后参数为时间(秒)
def sine_mouse_wave():
    """
    Moves the mouse in a sine wave from the left edge of
    the screen to the right.
    """
    width, height = autopy.screen.size()
    height /= 2
    height -= 10  # Stay in the screen bounds.

    for x in range(int(width)):
        y = int(height * math.sin((TWO_PI * x) / width) + height)
        autopy.mouse.move(x, y)
        time.sleep(random.uniform(0.001, 0.003))
sine_mouse_wave()

键盘类似
值得注意的是由于未知原因, autopy.key.Code.TAB缺失导致无法打出该按键

截屏

编写特殊用途脚本的时候, 有时需要获取屏幕状态/特定点集的颜色

import autopy
scn =  autopy.bitmap.capture_screen() #返回的是bitmap对象
color = scn.get_color(1, 1) #可读性低
print(hex(color))
print(autopy.color.hex_to_rgb(color))
scn.save(pathname)
scn = autopy.bitmap.Bitmap.open(pathname)
pos = scn2.find_bitmap(scn1) #模板匹配
pos = scn2.find_bitmap(scn1, rect=((10,10),(100,100)) #	((x, y), (width, height))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值