pip
安装就好了pip install autopy
AutoPy
有6
个功能模块6
个6
个__init__.py
中也可以看出来# -*- coding: utf-8 -*-
"""
AutoPy is a simple, cross-platform GUI automation library for Python.
"""
from . import alert, bitmap, color, key, mouse, screen
__author__ = "Michael Sanders"
__version__ = "4.0.0"
__all__ = ["alert", "bitmap", "color", "key", "mouse", "screen"]
6
个模块
alert
bitmap
color
key
mouse
screen
官方是这么介绍的
官方文档只给出了1
个函数
def alert(msg: str, title: str=None, default_button: str=None, cancel_button: str=None)
Displays alert with the given attributes. If cancel_button
is not given, only the default button is displayed. Returns True
if the default button was pressed, or False
if cancelled. Note that the arguments are keywords, and can be passed as named parameters (e.g., alert(msg='bar', title='foo')
).
显示具有给定属性的警报。 如果未提供cancel_button
,则仅显示默认按钮。 如果按下默认按钮,则返回True
;如果取消,则返回False
。 请注意,参数是关键字,可以作为命名参数传递(例如alert(msg ='bar',title ='foo')
)。
简单的说 有4
参数 有返回值True
或False
当有第4
个参数的时候会有俩按钮
让我们调用一下康康
from autopy import alert
def main():
alert.alert("你好", "弹出层", cancel_button="不")
if __name__ == '__main__':
main()
但是cancel_button
的值和按钮上的文字没有关系…
from autopy import alert
def main():
alert.alert("你好")
if __name__ == '__main__':
main()
就一个按钮
官方是这么介绍的
Bitmap
for accessing bitmaps and searching for bitmaps on-screen.Bitmap
,用于访问位图和搜索屏幕上的位图。官方文档给出了1
个类和1
个函数
类
class autopy.bitmap.Bitmap
首先说一下该类的一些字段吧
bounds
((x, y), (width, height))
size
(width, height)
width
float
height
float
scale
1.25
对象方法
@classmethod
def open(path: str) → Bitmap
Open the image located at the path specified. The image’s format is determined from the path’s file extension.
打开位于指定路径的图像。 图片的格式取决于路径的文件扩展名。
可以看出这是个classmethod
让我们调用一下康康
from autopy import bitmap
def main():
bmp = bitmap.Bitmap.open("./image/bmp.png")
print(bmp.bounds)
print(bmp.size)
print(bmp.width)
print(bmp.height)
print(bmp.scale)
if __name__ == '__main__':
main()
((0.0, 0.0), (153.0, 299.0))
(153.0, 299.0)
153.0
299.0
1.0
可以看出成功输出的图片的基本数据
def save(path: str, format: str=None)
Saves image to absolute path in the given format. The image type is determined from the filename if possible, unless format is given. If the file already exists, it will be overwritten. Currently only jpeg
and png
files are supported.
以给定格式将图像保存到绝对路径。 图像类型是根据文件名确定的,除非给出了格式。 如果文件已经存在,它将被覆盖。 当前仅支持jpeg
和png
文件。
IOError
is thrown if the file could not be saved.
ValueError
is thrown if image couldn’t be parsed.
如果无法保存文件,则抛出IOError
。
如果无法解析图像,则抛出ValueError
。
把图片保存到文件嘛
让我们调用一下康康
from autopy import bitmap
def main():
bmp = bitmap.Bitmap.open("./image/bmp.png")
bmp.save("./image/bmp1.png")
if __name__ == '__main__':
main()
可以看出成功保存了新的文件bmp1.png
def copy_to_pasteboard()
IOError
is thrown if the image could not be copied.ValueError
is thrown if the image was too large or small.IOError
。ValueError
。Windows
这个就没法子试了def point_in_bounds(x: float, y: float) → bool
Returns True
if the given point is contained in bmp.bounds
.
如果给定点包含在bmp.bounds
中,则返回True
。
判断给定坐标点是否在图片中
让我们调用一下康康
from autopy import bitmap
def main():
bmp = bitmap.Bitmap.open("./image/bmp.png")
print(bmp.point_in_bounds(100, 100))
print(bmp.point_in_bounds(600, 600))
if __name__ == '__main__':
main()
True
False
可以看出第一个点在图片中 第二个点不在图片中
def rect_in_bounds(rect: Tuple[Tuple[float, float], Tuple[float, float]]) → bool
Returns True
if the given rect
of the form ((x, y), (width, height))
is contained in bmp.bounds
.
如果bmp.bounds
中包含形式((x,y), (width,height))
的给定rect
,则返回True
。
判断给定矩形是否在图片中
让我们调用一下康康
from autopy import bitmap
def main():
bmp = bitmap.Bitmap.open("./image/bmp.png")
print(bmp.rect_in_bounds(((50, 50), (100, 100))))
print(bmp.rect_in_bounds(((100, 100), (600, 600))))
if __name__ == '__main__':
main()
True
False
可以看出第一个矩形在图片中 第二个矩形不在图片中
def get_color(x: float, y: float) → Tuple[int, int, int]
ValueError
is thrown if the point out of bounds.ValueError
。from autopy import bitmap
def main():
bmp = bitmap.Bitmap.open("./image/bmp.png")
color = bmp.get_color(100, 100)
print(hex(color))
if __name__ == '__main__':
main()
0x75788f
def find_color(color: Tuple[int, int, int], tolerance: float=None, rect: Tuple[Tuple[float, float], Tuple[float, float]]=None, start_point: Tuple[float, float]=None) → Tuple[float, float]
color
inside rect
of the form ((x, y), (width, height))
in bmp
from the given start_point
. Returns coordinates if found, or None
if not. If rect
is None
, bmp.bounds
is used instead. If start_point
is None
, the origin of rect
is used.Tolerance
is defined as a float in the range from 0
to 1
, where 0
is an exact match and 1
matches anything.start_point
在bmp
中((x, y), (width, height))
的rect
内部查找颜色
。 如果找到,则返回坐标Tuple[float, float]
;否则,返回None
。 如果rect
为None
,则使用bmp.bounds
。 如果start_point
为None
,则使用rect
的原点。tolerance
定义为介于0
到1
之间的浮点数,其中0
是完全匹配,而1
则匹配任何东西。颜色值
相似度
范围
起始点
返回None
或者坐标Tuple[float, float]
bmp.png
bmp.png
中找到0x75788f
from autopy import bitmap
def main():
bmp = bitmap.Bitmap.open("./image/bmp.png")
result = bmp.find_color(0x75788f)
print(result)
if __name__ == '__main__':
main()
(4.0, 117.0)
(100, 100)
的颜色值 但是这里给出的是(4, 117)
def find_every_color(color: Tuple[int, int, int], tolerance: float=None, rect: Tuple[Tuple[float, float], Tuple[float, float]]=None, start_point: Tuple[float, float]=None) → List[Tuple[float, float]]
(x, y)
coordinates inside rect
in bmp
matching color
from the given start_point
. If rect
is None
, bmp.bounds
is used instead. If start_point
is None
, the origin of rect
is used.start_point
开始返回bmp
中rect
内部所有匹配的颜色
的(x, y)
坐标的列表。 如果rect
为None
,则使用bmp.bounds
。 如果start_point
为None
,则使用rect
的原点。find_color()
只不过这个函数返回一个list
存放所有符合的点(100, 100)
在不在里面就好from autopy import bitmap
def main():
bmp = bitmap.Bitmap.open("./image/bmp.png")
result = bmp.find_every_color(0x75788f)
print("number:", len(result))
index = result.index((100.0, 100.0))
print("(100, 100):", index)
if __name__ == '__main__':
main()
number: 470
(100, 100): 253
470
个结果 (100, 100)
在253
def count_of_color(color: Tuple[int, int, int], tolerance: float=None, rect: Tuple[Tuple[float, float], Tuple[float, float]]=None, start_point: Tuple[float, float]=None) → int
len(find_every_color(color, tolerance, rect, start_point))
len(find_every_color(color, tolerance, rect, start_point))
from autopy import bitmap
def main():
bmp = bitmap.Bitmap.open("./image/bmp.png")
print(bmp.count_of_color(0x75788f))
if __name__ == '__main__':
main()
470
def find_bitmap(needle: Bitmap, tolerance: float=None, rect: Tuple[Tuple[float, float], Tuple[float, float]]=None, start_point: Tuple[float, float]=None) → Tuple[float, float]
needle
inside rect
in bmp from
the given start_point
. Returns coordinates if found, or None
if not. If rect
is None
, bmp.bounds
is used instead. If start_point
is None
, the origin of rect
is used.Tolerance
is defined as a float in the range from 0
to 1
, where 0
is an exact match and 1
matches anything.start_point
在bmp
中的rect
内找到图片
。 如果找到,则返回坐标Tuple[float, float]
;否则,返回None
。 如果rect
为None
,则使用bmp.bounds
。 如果start_point
为None
,则使用rect
的原点。tolerance
定义为介于0
到1
之间的浮点数,其中0
是完全匹配,而1
则匹配任何东西。bmp.png
和tim.png
bmp.png
包含六个图片 第二张图片tim.png
是Tim
的一小块bmp.png
中找到tim.png
的位置from autopy import bitmap
def main():
bmp = bitmap.Bitmap.open("./image/bmp.png")
tim = bitmap.Bitmap.open("./image/tim.png")
result = bmp.find_bitmap(tim)
print(result)
if __name__ == '__main__':
main()
(23.0, 115.0)
(23, 115)
用ps
看一下结果对不对在图层上放一个(23, 115)
左上角在(0, 0)
点的框
框的右下角在Tim
图标上 说明成功找到了
def find_every_bitmap(needle: Bitmap, tolerance: float=None, rect: Tuple[Tuple[float, float], Tuple[float, float]]=None, start_point: Tuple[float, float]=None) → [Tuple[float, float]]
(x, y)
coordinates inside rect
in bmp
matching needle
from the given start_point
. If rect
is None
, bmp.bounds
is used instead. If start_point
is None
, the origin of rect
is used.start_point
返回bmp
中rect
内部的所有与图片
匹配的(x, y)
坐标的列表。 如果rect
为None
,则使用bmp.bounds
。 如果start_point
为None
,则使用rect
的原点。list
from autopy import bitmap
def main():
bmp = bitmap.Bitmap.open("./image/bmp.png")
tim = bitmap.Bitmap.open("./image/tim.png")
result = bmp.find_every_bitmap(tim)
print(len(result))
print(result)
if __name__ == '__main__':
main()
1
[(23.0, 115.0)]
def count_of_bitmap(needle: Bitmap, tolerance: float=None, rect: Tuple[Tuple[float, float], Tuple[float, float]]=None, start_point: Tuple[float, float]=None) → int
needle
in bmp
. Functionally equivalent to:len(find_every_bitmap(color, tolerance, rect, start_point))
bmp
中出现图片
的次数。 功能等同于:len(find_every_bitmap(color, tolerance, rect, start_point))
from autopy import bitmap
def main():
bmp = bitmap.Bitmap.open("./image/bmp.png")
tim = bitmap.Bitmap.open("./image/tim.png")
print(bmp.count_of_bitmap(tim))
if __name__ == '__main__':
main()
1
def cropped(rect: Tuple[Tuple[float, float], Tuple[float, float]]) → Bitmap
ValueError
is thrown if the portion was out of bounds.ValueError
。ps
了from autopy import bitmap
def main():
bmp = bitmap.Bitmap.open("./image/bmp.png")
tim = bitmap.Bitmap.open("./image/tim.png")
result = bmp.find_bitmap(tim)
if result:
result_bmp = bmp.cropped((result, tim.size))
result_bmp.save("./image/result.png")
else:
print("Not found!")
if __name__ == '__main__':
main()
def is_bitmap_equal(bitmap: Bitmap, tolerance: float=None) → bool
True
if bitmap is equal to receiver with the given tolerance.True
。tim.png
一张是找到后截取出来的result.png
from autopy import bitmap
def main():
tim = bitmap.Bitmap.open("./image/tim.png")
result = bitmap.Bitmap.open("./image/result.png")
print(tim.is_bitmap_equal(result))
if __name__ == '__main__':
main()
True
True
说明两张图片是一样的类结束了 还有一个函数
def capture_screen(rect: Tuple[Tuple[float, float], Tuple[float, float]]) → autopy.bitmap.Bitmap
rect
is None
. The rect
parameter is in the form of ((x, y), (width, height))
.rect
为None
,则返回整个显示。 rect
参数的形式为 ((x, y), (width, height))
。ValueError
is thrown if the rect is out of bounds.IOError
is thrown if the image failed to parse.ValueError
。IOError
。官方是这么介绍的
官方文档给出了2
个函数
def rgb_to_hex(red: int, green: int, blue: int) → int
Returns hexadecimal value of given RGB tuple. r
, g
, and b
must be in the range 0 - 255.
返回给定RGB元组的十六进制值。 r
,g
和b
必须在0-255的范围内。
RGB转十六进制
让我们调用一下康康
from autopy import color
def main():
h = color.rgb_to_hex(100, 100, 100)
print(hex(h))
if __name__ == '__main__':
main()
0x646464
可以看出成功将RGB转换成为了十六进制
def hex_to_rgb(hex: int) → Tuple[int, int, int]
(r, g, b)
of the RGB integer values equivalent to the given RGB hexadecimal value. r
, g
, and b
are in the range 0 - 255.(r, g, b)
。 r
,g
和b
的范围是0-255。from autopy import color
def main():
r = color.hex_to_rgb(0x646464)
print(r)
if __name__ == '__main__':
main()
(100, 100, 100)
官方是这么介绍的
官方文档给出了3
个函数
都是控制键盘的 没有监听键盘的 监听键盘还需要用到其他的库 会在之后的文章中介绍
def toggle(key: Any, down: bool, modifiers: List[Modifier]=[], modifier_delay: float=None)
down
is True
, or releases it if not. Integer keycodes and modifiers should be taken from module constants (e.g., Code.DELETE
or Modifier.META
). If the given key is a character, it is automatically converted to a keycode corresponding to the current keyboard layout.down
为True
,则按住给定的键或键码;否则,则释放它。 整数键码和修饰符应从模块常量中获取(例如Code.DELETE
或Modifier.META
)。 如果给定的键是一个字符,它将自动转换为与当前键盘布局相对应的键代码。def tap(key: Any, modifiers: List[Modifier]=[], delay: float=None)
toggle()
that holds down and then releases the given key and modifiers.toggle()
进行封装,按住并释放给定的键和修饰符。from autopy import key
import time
import threading
def tap():
time.sleep(2)
for i in range(9):
key.tap('*')
if (i + 1) % 3 == 0:
key.tap(key.Code.SPACE)
def main():
threading.Thread(target=tap).start()
input("Please input: ")
if __name__ == '__main__':
main()
def type_string(string: str, wpm: float=None)
WPM
, or as fast as possible if the WPM
is 0
.WPM
键入字符串,如果WPM
为0
,则尝试尽快键入。from autopy import key
import time
import threading
def tap():
time.sleep(2)
key.type_string("|=> *** <=|")
def main():
threading.Thread(target=tap).start()
input("Please input: ")
if __name__ == '__main__':
main()
按键 |
---|
Code.F1 |
Code.F2 |
Code.F3 |
Code.F4 |
Code.F5 |
Code.F6 |
Code.F7 |
Code.F8 |
Code.F9 |
Code.F10 |
Code.F11 |
Code.F12 |
Code.F13 |
Code.F14 |
Code.F15 |
Code.F16 |
Code.F17 |
Code.F18 |
Code.F19 |
Code.F20 |
Code.F21 |
Code.F22 |
Code.F23 |
Code.F24 |
Code.ALT |
Code.BACKSPACE |
Code.CAPS_LOCK |
Code.CONTROL |
Code.DELETE |
Code.DOWN_ARROW |
Code.END |
Code.ESCAPE |
Code.HOME |
Code.LEFT_ARROW |
Code.META |
Code.PAGE_DOWN |
Code.PAGE_UP |
Code.RETURN |
Code.RIGHT_ARROW |
Code.SHIFT |
Code.SPACE |
Code.UP_ARROW |
Modifier.META |
Modifier.ALT |
Modifier.CONTROL |
Modifier.SHIFT |
官方是这么介绍的
官方文档给出了5
个函数
def location() -> (float, float)
Returns a tuple (x, y)
of the current mouse position.
返回当前鼠标位置的元组 (x, y)
。
让我们调用一下康康
from autopy import mouse
def main():
print(mouse.location())
if __name__ == '__main__':
main()
(686.4, 128.8)
输出的鼠标的当前位置
def toggle(button: Button=None, down: bool)
LEFT
, RIGHT
, MIDDLE
, or None
to default to the left button.LEFT
,RIGHT
,MIDDLE
或None
,默认为左按钮。def click(button: Button=None, delay: float=None)
toggle()
进行封装,按住并释放给定的鼠标按钮。 默认情况下,按下左按钮。from autopy import mouse
def main():
mouse.click()
if __name__ == '__main__':
main()
def move(x: float, y: float)
(x, y)
coordinate.(x, y)
坐标。ValueError
is thrown if the point is out of index.ValueError
。from autopy import mouse
def main():
mouse.move(100, 100)
if __name__ == '__main__':
main()
def smooth_move(x: float, y: float)
(x, y)
coordinate in a straight line.(x, y)
坐标。ValueError
is thrown if the point is out of index.ValueError
。from autopy import mouse
def main():
mouse.smooth_move(100, 100)
if __name__ == '__main__':
main()
按键 |
---|
LEFT |
RIGHT |
MIDDLE |
官方是这么介绍的
官方文档给出了4
个函数
def scale() → float
def size() -> (float, float)
(width, height)
of the size of the main screen in points.(width, height)
。def is_point_visible(x: float, y: float) → bool
True
if the given point is inside the main screen boundaries.True
。def get_color(x: float, y: float) -> (int, int, int)
让我们统一调用一下康康
from autopy import screen
def main():
print(screen.scale())
print(screen.size())
print(screen.is_point_visible(100, 100))
print(hex(screen.get_color(100, 100)))
if __name__ == '__main__':
main()
1.25
(1536.0, 864.0)
True
0xffffff
AutoPy
就介绍到这里了AutoPy
找图就会找不到