A simple, cross-platform GUI automation library for Python.
Project description
AutoPy Introduction and Tutorial
Introduction
AutoPy is a simple, cross-platform GUI automation library for Python. It includes functions for controlling the keyboard and mouse, finding colors and bitmaps on-screen, and displaying alerts.
Currently supported on macOS, Windows, and X11 with the XTest extension.
Getting Started
Requirements
- Python 2.7, or Python 3.5 and up.
- Rust 1.23.0-nightly 2019-02-06 or later (unless using a binary wheel distribution).
- macOS 10.6 and up.
- Windows 7 and up.
- X11 with the XTest extension.
Installation
First, see if a binary wheel is available for your machine by running:
$ pip install -U autopy
If that fails, install rustup and then run:
$ rustup default nightly
$ pip install -U setuptools-rust
$ pip install -U autopy
Another option is to compile from the latest source on the GitHub repository:
$ git clone git://github.com/autopilot-rs/autopy-rs.git
$ cd autopy
$ rustup default nightly
$ pip install -r requirements.txt
$ python setup.py build
# python setup.py install
Additional instructions for installing from source on Windows are available here.
Hello World
The following is the source for a "hello world" script in autopy. Running this code will cause an alert dialog to appear on every major platform:
import autopy
def hello_world():
autopy.alert.alert("Hello, world")
hello_world()
Tutorials
Controlling the Mouse
AutoPy includes a number of functions for controlling the mouse. For a full list, consult the API Reference. E.g., to immediately "teleport" the mouse to the top left corner of the screen:
>>> import autopy
>>> autopy.mouse.move(1, 1)
To move the mouse a bit more realistically, we could use:
>>> import autopy
>>> autopy.mouse.smooth_move(1, 1)
Even better, we could write our own function to move the mouse across the screen as a sine wave:
import autopy
import math
import time
import random
import sys
TWO_PI = math.pi * 2.0
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()
Working with Bitmaps
All of autopy's bitmap routines can be found in the module autopy.bitmap
. A
useful way to explore autopy is to use Python's built-in help()
function, for
example in help(autopy.bitmap.Bitmap)
. AutoPy's functions are documented with
descriptive docstrings, so this should show a nice overview.
>>> import autopy
>>> autopy.bitmap.capture_screen()
<Bitmap object at 0x12278>
This takes a screenshot of the main screen, copies it to a bitmap, displays its memory address, and then immediately destroys it. Let's do something more useful, like look at its pixel data:
>>> import autopy
>>> autopy.bitmap.capture_screen().get_color(1, 1)
15921906
AutoPy uses a coordinate system with its origin starting at the top-left, so
this should return the color of pixel at the top-left corner of the screen. The
number shown looks a bit unrecognizable, but we can format it with Python's
built-in hex
function:
>>> import autopy
>>> hex(autopy.bitmap.capture_screen().get_color(1, 1))
'0xF2F2F2'
Alternatively, we can use:
>>> import autopy
>>> autopy.color.hex_to_rgb(autopy.screen.get_color(1, 1))
(242, 242, 242)
which converts that hex value to a tuple of (r, g, b)
values. (Note that
autopy.screen.get_color()
, used here, is merely a more convenient and
efficient version of autopy.bitmap.capture_screen().get_color()
.)
To save the screen capture to a file, we can use:
>>> import autopy
>>> autopy.bitmap.capture_screen().save('screengrab.png')
The filetype is either parsed automatically from the filename, or given as an optional parameter. Currently only jpeg and png files are supported.
>>> import autopy
>>> autopy.bitmap.Bitmap.open('needle.png')
<Bitmap object at 0x1001d5378>
Aside from analyzing a bitmap's pixel data, the main use for loading a bitmap is finding it on the screen or inside another bitmap. For example, the following prints the coordinates of the first image found in a bitmap (scanned from left to right, top to bottom):
import autopy
def find_image_example():
needle = autopy.bitmap.Bitmap.open('needle.png')
haystack = autopy.bitmap.Bitmap.open('haystack.png')
pos = haystack.find_bitmap(needle)
if pos:
print("Found needle at: %s" % str(pos))
find_image_example()
Projects using AutoPy
- AutoPyDriverServer, AutoPy through WebDriver or a webdriver-compatible server.
- guibot, A tool for GUI automation using a variety of computer vision and desktop control backends.
- spynner, Programmatic web browsing module with AJAX support for Python.
- SUMO, An open source, highly portable, microscopic and continuous road traffic simulation package designed to handle large road networks.
API Reference
Hope you enjoy using autopy! For a more in depth overview, see the API Reference.
For more information, see the GitHub Repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for autopy-2.1.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2797d4083fe820501b3b680f979250fd2a064c51db73ceb9a2479c0e64d01780 |
|
MD5 | e8e10cc72db103dd270a26a20ce296ce |
|
BLAKE2b-256 | e397774642617257138be0ba5196f132341e27bff8e5fae57543a287683ca402 |
Hashes for autopy-2.1.0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62b8af529c22ceeaf07a3cbc385cee7a2032574f7d7616c364f682e2ceb7f591 |
|
MD5 | 3f69626e8d1fd1e1366ecdaa946b2973 |
|
BLAKE2b-256 | f97d25c7ed33573ba0a33b08554a51b15eb1d16f418fe3036a971f47d1e145ff |
Hashes for autopy-2.1.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3b1a52bebb965664f60c13da8e1218f907be985abf18b37731fe415227b1185 |
|
MD5 | 9c732325b3c99f4e373fb1971d60caf2 |
|
BLAKE2b-256 | 14393f789641494b22031fc1f7d5295f847156beb7548b51035efa2cf570c8fb |
Hashes for autopy-2.1.0-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86dddec31c25af8c2ee096cc1e792344e0e2aabb3ce47100b5aba020a3634509 |
|
MD5 | e1932b212a553e50a46fef48f3b24c10 |
|
BLAKE2b-256 | 092e1d43962e8e7d340ee876faa4778ba2d8b1c498469ede4b95e75c48b32165 |
Hashes for autopy-2.1.0-cp37-cp37m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30c4cd6941aa664c6b9c874777779dbcf6c3d191aa0dd6d0989deda8b18534bc |
|
MD5 | 20255e49557008ee7b6623af80c19f7f |
|
BLAKE2b-256 | 67458f2945de4b6d493f9fd1aef734722d1751e7190a9de6d10cd56cb5ddb0a8 |
Hashes for autopy-2.1.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 931b29d389a28e47bc8d31cf856b6ed49da7cfdaab65fe5d5d8a5eef4111f79e |
|
MD5 | f930e25f6a810614414bdb89a3d91df4 |
|
BLAKE2b-256 | b558f38340882cdd208d8a7e571500c1e5054019e4a9743451edf69e3b063039 |
Hashes for autopy-2.1.0-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a4f623cb69c8873cc4fb890b2052499e930d74ae8a4a290605e8ea7748367a1 |
|
MD5 | 726fab5c47b2afbfac280e7c8acfcecd |
|
BLAKE2b-256 | 182b2c850a990599cad6670ddd0b882f1ab789e095c5b5b0e047ed4165599e9f |
Hashes for autopy-2.1.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77a6752beac009d355513e7c077f96e81ed7dfb3fd91e9a42e0cb8f4248ea15e |
|
MD5 | 978b3291aeba07aead98ae33961439e5 |
|
BLAKE2b-256 | f94ebda236ffd2d0bafcf5232aa9d0baa06adceb847da058580ef51677d102d6 |
Hashes for autopy-2.1.0-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc24ebef9e12b5e25759b0dbcecd75afe58eabe6e625e1b8656ab21d82aed24a |
|
MD5 | 0334cf626ecc8ade35e1d36784490e7e |
|
BLAKE2b-256 | 5c0ad427c3e81486c175d022abb251471048d16a8a2daae25acef022832cad08 |
Hashes for autopy-2.1.0-cp36-cp36m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00e8dbec8fd19d6fbffa2094f51e96151d2d30626f78986722474a727c8cca83 |
|
MD5 | 7b172bc983abf9a4a04b090dfb626c87 |
|
BLAKE2b-256 | 765f522bf833a49e9291aadd42976860396a3f6731f28525cdfbd1cfc58f8beb |
Hashes for autopy-2.1.0-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d273633e3d855161a899b4b70d3f9891e763de8244555e1f7fcaae4e146ede1 |
|
MD5 | d6a380d503f9a6bd28036a63817e6727 |
|
BLAKE2b-256 | 0dc6b52ef420ee2f55e80342f94a1adc1c75e3c6a081b06d373143968d130c42 |
Hashes for autopy-2.1.0-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce68e52a833edd1a545dae45c72074d08ab67086d63a20ccdbefe5f1272bd1dd |
|
MD5 | 226a2613bad41d7b32772aec4b4ce68d |
|
BLAKE2b-256 | 583ebedead21c12c05b89706f07b460852651198dc0ff6acb82dd15158b590c6 |
Hashes for autopy-2.1.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb9790797c9afd2c5b9c2fa8b6796dfadcbc3fc28a712ff4f70d85bf0f537632 |
|
MD5 | bed492975268ada307b0582f7d789faf |
|
BLAKE2b-256 | 6c4df60e581a03fbec9438d62a542f3e73dc609e83420944d00641863e68616d |
Hashes for autopy-2.1.0-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24e5a1ac682b40fe895fe24d7ab5aeb8e3bc5edcb51d239ce8281ac5f7c3358a |
|
MD5 | 3f8f553a5a4a56ec21f8e818c46cb822 |
|
BLAKE2b-256 | 99741d1a1fd023345f6b5925850d776df1572f7904e8a1d1979d1b9cea037131 |
Hashes for autopy-2.1.0-cp35-cp35m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ef98a6d9929add532a91f4f81a0c077d32f7107a0a3755c05bcb329d91b0e60 |
|
MD5 | 0b00dbda5242c56222b7280ab2a1b3cf |
|
BLAKE2b-256 | 7ffd76d1b60e81c1b1eb5b47f1ce6dc0f300239547ea77f8a52d2f212a6dea9f |
Hashes for autopy-2.1.0-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 652f0dfb97f6ac1992c4e002c5f7b22b29ad55f966b9647d6c7dd0e3aa85720d |
|
MD5 | 4098a237efefc97b6d4f788423e3ede6 |
|
BLAKE2b-256 | c46cac82c33b91cc12922d62231faedbd7ff5e5973656e27afb5eacc73cf3469 |
Hashes for autopy-2.1.0-cp27-cp27mu-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2260038d72cfb14525b2f63525d557552103dbd4f96631ad826093695fa2d26e |
|
MD5 | 284b4d88bc8d4da5aafddf5aa73bdfd1 |
|
BLAKE2b-256 | 5f00434ec12edc921dc151d515441f27b42d93e1de9984b05c30fedeaab0ccd4 |
Hashes for autopy-2.1.0-cp27-cp27m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79b03c20bf7472b92e228104231a14b824dd912f76abe1f706ee3fe3e297a080 |
|
MD5 | 4a5a38416fc95d0cef64e3fb2ccc18af |
|
BLAKE2b-256 | c9ec5840184518c56f4a516576bb59a356f02c70cdbd34110e5f8b5dd2346c06 |
Hashes for autopy-2.1.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba7594088ad12a763960972935d505ed4f3c9a17e296756029603f16e1c40d9b |
|
MD5 | d5b90bca731a243dbb4b039ce117ded8 |
|
BLAKE2b-256 | b8b7591f3f01218af14c498184ea9b9f2186b3962dbb1dc1e9d02f83c136878b |
Hashes for autopy-2.1.0-cp27-cp27m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e06d335ee097b72da56806031d599891cbdf7e862e9956f8c99c50b2c74348e2 |
|
MD5 | e3f9623a2ffbc9882f6b6fd1722b240f |
|
BLAKE2b-256 | d00fe080ce9d1526fe6031d8469e802c7c01813e025e68ecb7e56aab8607b4ee |
Hashes for autopy-2.1.0-cp27-cp27m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a77a2b5e1ea4dbac24094c0122bb983c766479506bab72be780fd1c6a00157e6 |
|
MD5 | 02106e61d8d87ea7581f076163222630 |
|
BLAKE2b-256 | 42964ac33e788987ac28715e92935ff539d45a1277d6f7628715fa1b13bf2e12 |