一、AutoPy的介绍和教程

1.1什么是AutoPy?

AutoPy是一个简单的跨平台GUI自动化工具包,适用于Python。它包括用于控制键盘和鼠标,在屏幕上查找颜色和位图以及显示警报的功能 - 所有这些都是以跨平台,高效和简单的方式进行的。适用于Mac OS X,Windows和X11。

 

二、AutoPy入门

2.1安装:

1、Windows安装包地址:http://pypi.python.org/pypi/autopy/.

2、使用easy_install安装:

Easy_install autopy

3、手动编译GitHub仓库的源码:

git clone git://github.com/msanders/autopy.git

cd autopy

python setup.py build

python setup.py install

2.2Hello World

代码如下:

def  hello_theme_world():
    autopy.alert.alert("hello,world")
hello_theme_world()

运行结果:

 

 

三、教程

3.1控制鼠标

AutoPy包含许多控制鼠标的功能。Document地址:http://www.autopy.org/documentation/

3.2控制鼠标简要教程

Autopy.mouse.move()

import autopy
autopy.mouse.move(1,1)

将鼠标快速移动到指定位置,尽管只导入了autopy,仍然可以使用autopy.mouse模块。为了更真实的移动鼠标,我们使用下面的方法。

Autopy.mouse.smooth_move()

import autopy
autopy.mouse.smooth_move(1,1)

我们可以编写我们自己的函数让鼠标在屏幕上做正弦波移动

import autopy
import math
import time
import random
TWO_PI=math.pi*2.0
def sine_mouse_wave():
    '''
让鼠标以正弦波的形式从屏幕左边移到右边
    '''
    
width,height=autopy.screen.get_size()
    height/=2
    height-=10 #留在屏幕边界
    for x in xrange(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()

 

3.3使用位图

所有autopy的bitmap都能在autopy.bitmap模块中扎到(准确的说,大多数都在autop.bitmap.Bitmap类中找到),我们可以使用Python内置的help()函数来探索autopy,例如help(autopy.bitmap.Bitmap)。

目前有三种方式在autopy中加载位图:

1)截取屏幕截图

2)加载文件

3)解析字符串

3.3.1第一种方法最容易的如:

>>> import autopy

>>> autopy.bitmap.capture_screen()

<Bitmap object at 0x0000000002558BC0>

我们获取像素数据

>>> import autopy

>>> autopy.bitmap.capture_screen().get_color(1,1)

6184542

AutoPy将整个屏幕建立为一个坐标系,其起点在左上角,所以上诉代码返回的是屏幕左上角的像素的颜色。我们现在将这个返回的结果以16进制的形式显示

>>> import autopy

>>> hex(autopy.bitmap.capture_screen().get_color(1,1))

'0x5e5e5e'

显然这是一个RGB的十六进制值,与HTML与CSS中使用的相同。我们也可以用RGB的形式显示

>>> import autopy

>>> autopy.color.hex_to_rgb(autopy.screen.get_color(1,1))

(0, 0, 0)

将改16进制值转换为(r,g,b)值的元组。这里使用autopy.screen.get_color(1,1)只是autopy.bitmap.capture_screen().get_color(1,1)一个更方便更有效的版本。

保存获取到的图片

>>> import autopy

>>> autopy.bitmap.capture_screen().save('lfsenior.png')

文件类型是从文件名自动解析,或作为可选参数给出。AutoPy目前只支持BMP和PNG文件类型

 

3.3.2加载位图

加载位图的方法基本上是相同的

>>> import autopy

>>> autopy.bitmap.Bitmap.open('lfsenior.png')

<Bitmap object at 0x000000000264D058>

 

3.3.3加载字符串

有时候需要让一个简短的脚本没有任何外部依赖。在位图的情况下,可以使用to_string()和from_string()方法完成。

>>> autopy.bitmap.Bitmap.open('lfsenior.png').to_string()

'b105,42,eNrtwTEBAAAAwqD1T20JT6AAAAAAAAAAAAAAAACAjwEzrgAB'

>>> autopy.bitmap.Bitmap.from_string('b105,42,eNrtwTEBAAAAwqD1T20JT6AAAA

AAAAAAAAAAAACAjwEzrgAB')

<Bitmap object at 0x000000000264D058>

这种方式不建议用于大型位图(例如,屏幕截图显然太大了),但对于想要非常容易的在脚本中使用段图像可能很有用。

 

除了分析位图数据,这个函数还能加载一个位图,查找他在屏幕上或另一个位图中的位置,如:查找电脑屏幕中回收站的位置

import autopy
def where_is_the_rubbish():
    '''查找回收站在屏幕中的位置'''
    
rubbish=autopy.bitmap.Bitmap.open('rubbish.png')
    screen=autopy.bitmap.capture_screen()
    pos=screen.find_bitmap(rubbish)
    if pos:
        print '找到了,他的位置在:%s' % str(pos)
    else:
        print '没有找到'
where_is_the_rubbish()