Skip to main content

maa-capture

maa-capture 的 Python 绑定,使用标准库 ctypes,提供原生截图方式、测速管理器和自定义截图回调。

安装与平台

pip install --index-url https://pypi.org/simple maa-capture
pip install "maa-capture[numpy]"  # 可选

需要 Python 3.12+。官方 wheel 内置 Windows x64、Linux x64、macOS ARM64 原生库;其他架构需要自行构建并通过 set_lib_path() 配置。Linux 还需要 PipeWire、libdrm、Wayland 运行库;macOS 需要 macOS 14+ 和宿主的屏幕录制权限。

从源码 / editable 安装不会自动编译 C++ 库,需先构建本项目。详细要求见构建文档。

Windows 整屏或窗口

from maa_capture import Manager, available_methods

print(hex(available_methods()))
with Manager(methods=Manager.METHOD_DXGI_DESKTOP_DUP) as capture:
    capture.set_monitor_index(0)  # 主显示器
    if not capture.connect():
        raise RuntimeError("无法连接截图来源")
    image = capture.screencap()
    if image is not None:
        print(capture.selected_unit_name(), image.width, image.height)
        pixels = image.data  # BGR bytes,已复制,可独立保留

指定窗口时可使用 Windows 专用的标题查找辅助:

from maa_capture import Manager, find_window_by_title

hwnd = find_window_by_title("记事本")
if not hwnd:
    raise RuntimeError("未找到窗口")
with Manager(hwnd=hwnd, methods=Manager.METHOD_BACKGROUND) as capture:
    if capture.connect():
        image = capture.screencap()

METHOD_BACKGROUND 为 FramePool | PrintWindow;METHOD_FOREGROUND 为 DXGI 窗口裁剪 | ScreenDC。可以自行按位或组合。伪最小化会临时修改窗口显示属性,关闭或停用时恢复。

macOS

from maa_capture import Manager

with Manager(hwnd=None, methods=Manager.METHOD_SCREENCAPTUREKIT) as capture:
    capture.set_monitor_index(0)
    if capture.connect():
        image = capture.screencap()

hwnd 可传 CGWindowID 的整数值进行窗口截图,None / 0 表示显示器。Windows 的窗口查找辅助不会在 macOS 导出;本包没有 macOS 窗口枚举或权限申请界面。

Linux

PipeWire 的授权和来源选择由调用方完成。以下 portal_fd 和 node_id 是调用方从 ScreenCast 会话取得的值:

from maa_capture import Manager

with Manager(methods=Manager.METHOD_PIPEWIRE) as capture:
    capture.set_pipewire_target(fd=portal_fd, node_id=node_id)
    if capture.connect():
        image = capture.screencap()

连接当前会话已存在的视频源时使用 fd=-1 和有效节点 ID,例如 gamescope 的节点。FD 在连接时交给 PipeWire 管理,不能重复关闭或复用;调用方还需保持授权会话存活。

Wlr 用于支持 wlr-screencopy 的 Wayland 合成器:

with Manager(methods=Manager.METHOD_WLR) as capture:
    capture.set_wlr_socket(None)  # WAYLAND_DISPLAY 默认 socket
    capture.set_monitor_index(0)
    if capture.connect():
        image = capture.screencap()

PipeWire 返回协商尺寸的整帧,窗口范围由上游来源决定;本库不二次裁剪,显示器索引也不改变 PipeWire 来源。Linux 的 hwnd 被忽略,内置后端没有通用 X11 窗口句柄截图接口。

显示器与方式查询

from maa_capture import Manager, available_methods

print(hex(available_methods()))
with Manager(methods=Manager.METHOD_NONE) as capture:
    for index in range(capture.monitor_count()):
        print(index, capture.monitor_info(index))

available_methods() 表示构建能力,实际权限和来源可用性由 connect() 验证。显示器查询无需先连接;无权限、无 Wayland 或相关后端未编入时,数量可为 0。monitor_info() 越界返回 None,Linux 名称目前为空。

自定义回调与像素生命周期

import ctypes
from maa_capture import ImageBuffer, Manager, ScreencapCallbacks

pixels = (ctypes.c_ubyte * 8)(10, 20, 30, 255, 40, 50, 60, 255)
def on_frame(_arg, out: ImageBuffer):
    out.data = ctypes.cast(pixels, ctypes.POINTER(ctypes.c_ubyte))
    out.width, out.height, out.channels, out.step = 2, 1, 4, 8
    return 1

callbacks = ScreencapCallbacks(screencap=on_frame)
with Manager(methods=Manager.METHOD_NONE) as capture:
    assert capture.register_custom_unit("gradient", callbacks)
    assert capture.connect()
    image = capture.screencap()
    assert image.data == bytes([10, 20, 30, 40, 50, 60])

像素缓冲应在原生调用复制完成前保持存活,不能只在回调内创建一个返回后立即失效的临时缓冲。管理器会保留已注册的回调和原生结构体引用。测速会调用回调多次,未选中的候选也会收到停用回调;停用实现应可重复调用。截图回调抛出的异常会记录到 Python 日志并转换为截图失败;其他回调也应自行处理异常。

关闭、线程与错误

优先用 with Manager(...)。也可显式 close(),它会停用并销毁原生管理器,可重复调用;关闭后的操作会抛出 RuntimeError。inactive() 只停用来源或恢复窗口状态,仍可继续使用管理器。

同一实例的配置、连接、截图和关闭需要串行化,GIL 不能替代原生对象同步。connect() 返回 False 表示没有候选成功;screencap() 返回 None 表示本次失败。成功调用也不保证业务画面没有黑屏或冻结,需由业务层判断。

ImageBuffer.Info.data 是 BGR 字节。安装 NumPy 扩展后,原始 ImageBuffer 的 to_numpy() 可转换图像;若使用 Manager 返回的 Info,可使用 numpy.frombuffer(image.data, dtype=numpy.uint8).reshape(image.height, image.width, 3)。

原生库加载顺序

  1. set_lib_path(path) 指定的文件或目录。
  2. MAA_WS_LIB,或兼容的 MAA_WS_DLL_DIR。
  3. wheel 的 _native/<platform>/ 内置库。
  4. 系统动态库搜索路径。

创建管理器或查询构建方式时会触发加载。失败异常包含尝试过的路径,系统依赖缺失也可能导致已有文件无法加载。

文档与许可

主仓库 · 截图方式 · 集成与所有权 · 故障排查

LGPL-3.0-or-later,提取代码来源为 MaaFramework,见随包提供的 LICENSE。

Metadata

Release files for maa-capture 1.0.0

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

Source distribution (sdist)

Source distribution for maa-capture 1.0.0
File Size Uploaded Provenance Actions
maa_capture-1.0.0.tar.gz 368.9 kB Without provenance Details for maa_capture-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for maa-capture 1.0.0
File Interpreter ABI Platform Actions
maa_capture-1.0.0-py3-none-any.whl Python 3 none any Details for maa_capture-1.0.0-py3-none-any.whl

Total release size: 736.2 kB

Release files / maa_capture-1.0.0.tar.gz

Download URL maa_capture-1.0.0.tar.gz
Size 368.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9e4cdc2b3e85505286ba833ae7f897adec0f0f99babf4f2907f19a73e1905824
BLAKE2b-256 checksum
How to use checksums
c7ca125102316ba4726882bdb21d7bcd662b5c42b625c070fd347de4ac94e911
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / maa_capture-1.0.0-py3-none-any.whl

Download URL maa_capture-1.0.0-py3-none-any.whl
Size 367.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
60951a50a0b5fdf44a42a289d80dbb8233f95fa55c10b6ddde0da8362a475f5a
BLAKE2b-256 checksum
How to use checksums
630e63f02a93df2cefa8fec8e4d30d532e8777f857326a8101bf1c2f5e58c1f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for maa-capture 1.0.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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