TRAA Python Bindings
Python bindings for the TRAA (To Record Anything Anywhere) library - a high-performance, cross-platform solution for screen capture, window enumeration, and desktop recording. Supports Windows, macOS, and Linux with a clean, Pythonic API.
Features
- Screen and window enumeration with filtering options
- High-performance screen capture
- Support for multiple displays and windows
- Thumbnail and icon capture
- Cross-platform compatibility (Windows, macOS, Linux)
Installation
pip install traa
Quick Start
from traa import Size, ScreenSourceFlags, enum_screen_sources, create_snapshot
from PIL import Image
sources = enum_screen_sources(
thumbnail_size=Size(160, 120),
icon_size=Size(32, 32),
external_flags=ScreenSourceFlags.NONE
)
for source in sources:
print(f"Found source: {source}")
if source.thumbnail_data is not None:
Image.fromarray(source.thumbnail_data).save(f"thumb_{source.id}.png")
if source.icon_data is not None:
Image.fromarray(source.icon_data).save(f"icon_{source.id}.png")
if sources:
image, actual_size = create_snapshot(sources[0].id, Size(1920, 1080))
mode = "RGB" if len(image.shape) == 3 and image.shape[2] == 3 else \
"RGBA" if len(image.shape) == 3 and image.shape[2] == 4 else "L"
Image.fromarray(image, mode=mode).save("snapshot.png")
print(f"Captured image size: {actual_size}")
API Reference
Functions
enum_screen_sources(icon_size: Optional[Size] = None, thumbnail_size: Optional[Size] = None, external_flags: ScreenSourceFlags = ScreenSourceFlags.NONE) -> List[ScreenSourceInfo]
Enumerates available screen sources (displays and windows).
icon_size
: Optional size for source icons
thumbnail_size
: Optional size for source thumbnails
external_flags
: Flags to control enumeration behavior
- Returns: List of
ScreenSourceInfo
objects
Example:
sources = enum_screen_sources(
thumbnail_size=Size(160, 120),
external_flags=ScreenSourceFlags.IGNORE_SCREEN | ScreenSourceFlags.IGNORE_MINIMIZED
)
create_snapshot(source_id: int, size: Size) -> Tuple[np.ndarray, Size]
Creates a snapshot of the specified screen source.
source_id
: ID of the source to capture
size
: Requested size of the snapshot
- Returns: Tuple of (image data as numpy array, actual capture size)
Example:
image, size = create_snapshot(window_id, Size(3840, 2160))
Image.fromarray(image, mode="RGB").save("4k_snapshot.png")
Classes
Size
Represents a size with width and height.
size = Size(width=1920, height=1080)
print(size)
size_4k = Size(3840, 2160)
Rect
Represents a rectangle with left, top, right, and bottom coordinates.
rect = Rect(left=0, top=0, right=1920, bottom=1080)
print(rect)
print(rect.width)
print(rect.height)
ScreenSourceInfo
Contains information about a screen source.
Properties:
id
: Unique identifier
is_window
: Whether this is a window or display
rect
: Source rectangle
title
: Source title
process_path
: Process path (windows only)
is_minimized
: Window minimized state
is_maximized
: Window maximized state
is_primary
: Whether this is the primary display
icon_data
: Optional icon image data (numpy array)
thumbnail_data
: Optional thumbnail image data (numpy array)
Example:
def print_source_info(source):
print(f"Source: {source.title}")
print(f"Type: {'Window' if source.is_window else 'Display'}")
print(f"Rectangle: {source.rect}")
if source.is_window:
print(f"Process: {source.process_path}")
print(f"Minimized: {source.is_minimized}")
else:
print(f"Primary Display: {source.is_primary}")
ScreenSourceFlags
Enumeration flags for controlling screen source enumeration behavior.
class ScreenSourceFlags(IntFlag):
NONE = 0
IGNORE_SCREEN = 1 << 0
IGNORE_WINDOW = 1 << 1
IGNORE_MINIMIZED = 1 << 2
NOT_IGNORE_UNTITLED = 1 << 3
NOT_IGNORE_UNRESPONSIVE = 1 << 4
IGNORE_CURRENT_PROCESS_WINDOWS = 1 << 5
NOT_IGNORE_TOOLWINDOW = 1 << 6
IGNORE_NOPROCESS_PATH = 1 << 7
NOT_SKIP_SYSTEM_WINDOWS = 1 << 8
NOT_SKIP_ZERO_LAYER_WINDOWS = 1 << 9
ALL = 0xFFFFFFFF
Common flag combinations:
displays = enum_screen_sources(
external_flags=ScreenSourceFlags.IGNORE_WINDOW
)
active_windows = enum_screen_sources(
external_flags=ScreenSourceFlags.IGNORE_SCREEN |
ScreenSourceFlags.IGNORE_MINIMIZED
)
all_sources = enum_screen_sources(
external_flags=ScreenSourceFlags.NOT_SKIP_SYSTEM_WINDOWS |
ScreenSourceFlags.NOT_IGNORE_TOOLWINDOW
)
Examples
See the examples
directory for more detailed examples:
Requirements
- Python 3.7+
- NumPy for array operations