Skip to main content

screen_objects

screen_objects is a Python extension for Android UI automation. It finds saved image samples on the current device screen, then checks, taps, swipes, counts, and calibrates those screen objects through ADB.

It is implemented in Rust with PyO3 and built with maturin.

Requirements

  • Python 3.8 or newer
  • Rust nightly, as configured in rust-toolchain.toml
  • maturin
  • Android Debug Bridge (adb)
  • An Android device or emulator with USB or wireless debugging enabled

Install

For local development:

pip install maturin
maturin develop

To build a wheel:

maturin build --release

Quick start

Put reference images in an objects directory. Each filename stem becomes an object name.

assets/
  objects/
    start_button.png
    close_icon.png

Configure the device before performing any device operation, then load the objects:

from pathlib import Path

from screen_objects import Direction, SwipeSpeed, device_config, get_objects

device_config(serial="emulator-5554")
objects = get_objects(Path("assets/objects"))

if objects["start_button"].tap(timeout=10.0):
    objects["close_icon"].swipe(Direction.Left, SwipeSpeed.Normal, duration=0.4)

device_config() uses adb from PATH by default. Pass the executable path when necessary:

device_config(Path("/path/to/adb"), serial="192.168.1.20:5555")

When serial is set, device_config() runs adb connect once and errors if that device is not listed. When serial is omitted, exactly one ready device must appear in adb devices; otherwise pass serial explicitly. It then runs adb wait-for-device and waits until sys.boot_completed is 1.

Samples and calibration

get_objects(path) loads every image below path and returns a dictionary of ScreenObject instances. Image stems must be unique, including across subdirectories. The function creates or updates objects.json next to the objects directory to store calibration data.

By default, an object is searched for anywhere on the screen. Calibrating it can improve matching:

objects["start_button"].calibrate()

Calibrate while the object is visible. This records a matching tolerance and continues to search the whole screen. Use fixed=True when the object always occupies the same screen position:

objects["close_icon"].calibrate(fixed=True)

Regions

Regions constrain object matching to part of the screen. Store their reference images separately, calibrate the region first, then load objects with the same regions directory:

from screen_objects import get_regions

regions_dir = Path("assets/regions")
objects_dir = Path("assets/objects")

regions = get_regions(regions_dir)
regions["main_panel"].calibrate()

objects = get_objects(objects_dir, regions_dir)
objects["reward_badge"].calibrate(region="main_panel")

Region data is saved as regions.json beside the regions directory. Call get_regions() before get_objects() when an application needs both collections.

Matching and actions

Lookups use a cached screenshot. Actions that change the device through this module clear that cache. If the screen changes outside this module, call reset_screen() before the next lookup.

timeout is measured in seconds. The default, 0.0, performs an immediate lookup. A positive value waits for a match until the timeout expires.

index selects a specific match and is zero-based: index=0 selects the first match. It can be used with exists(), tap(), swipe(), and calibrate(). Objects calibrated with fixed=True have one known position, so they do not support index, count(), or tap_each().

# Check for a match for up to five seconds.
if objects["reward_badge"].exists(timeout=5.0):
    # Tap the second match three times, waiting 0.2 seconds between taps.
    objects["reward_badge"].tap(index=1, times=3, interval=0.2)

# Tap every match in the current screenshot and receive the number tapped.
tapped = objects["collect_button"].tap_each()

Required actions

Every lookup or action that can return False has a force_* form. It raises RuntimeError and saves the current screenshot to screen-<device-serial>.png when no match is found.

objects["start_button"].force_tap(timeout=10.0)
objects["close_icon"].force_exists()

API reference

Object loading

get_objects(objects_dir: Path, regions_dir: Path | None = None) -> dict[str, ScreenObject]
get_regions(regions_dir: Path) -> dict[str, ScreenRegion]

ScreenObject

exists(timeout: float = 0.0, index: int | None = None) -> bool
force_exists(timeout: float = 0.0, index: int | None = None) -> None

tap(
    timeout: float = 0.0,
    index: int | None = None,
    times: int = 1,
    interval: float = 0.0,
) -> bool
force_tap(
    timeout: float = 0.0,
    index: int | None = None,
    times: int = 1,
    interval: float = 0.0,
) -> None

swipe(
    dir: Direction,
    speed: SwipeSpeed,
    duration: float,
    index: int | None = None,
) -> bool
force_swipe(
    dir: Direction,
    speed: SwipeSpeed,
    duration: float,
    index: int | None = None,
) -> None

count() -> int
tap_each() -> int
calibrate(
    fixed: bool = False,
    region: str | None = None,
    index: int | None = None,
) -> None

tap() returns False if the object is not found before timeout. times=0 performs no taps and returns True when the object is found; interval is the delay between repeated taps. swipe() begins at the matched point and returns False when there is no match. count() returns the number of current matches, and tap_each() returns the number of taps sent.

Device functions

device_config(
    adb: Path = Path("adb"),
    serial: str | None = None,
    app: str | None = None,
) -> None
start_app() -> None
close_app() -> None

reset_screen() -> None
screenshot() -> None
tap_center() -> None
swipe_center(dir: Direction, speed: SwipeSpeed, duration: float) -> None
back() -> None
home() -> None
write(text: str) -> None

device_config() must be called before other device operations. With serial, it connects once via adb connect and fails if the device is missing. Without serial, it requires exactly one ready device. It then waits with adb wait-for-device and until sys.boot_completed is 1. Pass app to select an Android package by name; start_app() launches it, and close_app() force-stops it.

screenshot() saves the cached or newly captured screen to screen-<device-serial>.png. tap_center(), swipe_center(), back(), and home() send their respective device action. write() enters text into the focused Android field. Call reset_screen() after an external or text-entry action when a fresh lookup is needed.

Enums

Direction.Left
Direction.Right
Direction.Up
Direction.Down

SwipeSpeed.Slow
SwipeSpeed.Normal
SwipeSpeed.Fast
SwipeSpeed.Turbo

Development

Run Rust tests with:

cargo test

Build the extension into the active Python environment with:

maturin develop

Metadata

Release files for screen-objects 6.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for screen-objects 6.0.7
File Interpreter ABI Platform Actions
screen_objects-6.0.7-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details for screen_objects-6.0.7-cp314-cp314-win_amd64.whl
screen_objects-6.0.7-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.17+ x86-64 Details for screen_objects-6.0.7-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Total release size: 955.6 kB

Release files / screen_objects-6.0.7-cp314-cp314-win_amd64.whl

Download URL screen_objects-6.0.7-cp314-cp314-win_amd64.whl
Size 420.8 kB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
38a863488ca4e057ba05ec8627ce8eeb287a66a234278c30dd3d81985f314712
BLAKE2b-256 checksum
How to use checksums
a5cd084814c3477bd769ce1eff659098a66bc6dfb4c0c5eda3be16eb1fe379cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

Release files / screen_objects-6.0.7-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL screen_objects-6.0.7-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 534.9 kB
Tags CPython 3.14 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
be55b551419ed149b525d2fb300afd0188ad3cb6453e1ed046506ebabeaf4486
BLAKE2b-256 checksum
How to use checksums
9c29309ec1514c95dc48317101c768f2a0a213a188629eee9ab78c05c839c3cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

Security for screen-objects 6.0.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for screen_objects-6.0.7-cp314-cp314-win_amd64.whl

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page