Skip to main content

Python 绑定

对 C ABI 共享库(mandelbrot_explorer.dll / libmandelbrot_explorer.so / libmandelbrot_explorer.dylib)的轻量 封装:ctypes + numpy,无需编译步骤,颜色与迭代数组都是零拷贝(库直接写入 numpy 缓冲)。

安装

使用 wheel

CI(.github/workflows/python-wheels.yml)为 Linux / Windows / macOS 构建 wheel,wheel 内已捆绑对应平台的 核心库;Linux wheel 在 manylinux_2_28 容器内构建,兼容较旧的 glibc。推送 v* tag 时会自动创建 Release 并发布到 PyPI(Trusted Publishing):

pip install mandelbrot-explorer                 # PyPI 首发后可用
# 或从 Release 下载对应平台的 wheel:
pip install mandelbrot_explorer-*-py3-none-*.whl

wheel 不包含 OpenCV 版的视频库;write_video 需要另行构建 mandelbrot_explorer_video 并用环境变量 MANDELBROT_EXPLORER_VIDEO_LIBRARY 指向它(见第 4、5 节)。

从源码安装

先按仓库 README 构建共享库,再把库拷进包目录并安装:

cmake --build build --config Release
python bindings/python/stage_libraries.py --build-dir build   # 需要视频库时加 --video
pip install bindings/python

也可以完全不安装:在仓库内直接运行(把 bindings/python 加入 sys.path 或从该目录运行), 绑定会自动在 build/ 下查找库(见下节)。

使用

先构建共享库(mandelbrot_explorer 目标),然后:

import mandelbrot_explorer as me

with me.Mandelbrot(1280, 720, max_iterations=1000) as m:   # threads 0 = auto
    m.set_view(4.0, -0.743643887037158704752191506114774,
               0.131825904205311970493132056385139)
    colors, iterations = m.render(colors=True, iterations=True)    # (h, w, 3) uint8 / (h, w) int32
    m.set_pixel_order("rgb")
    rgb = m.render()
    m.write_image("frame.bmp")                                      # bmp / pnm,无外部依赖

一次调用(不需要 context):

colors = me.render_frame(1920, 1080, max_iterations=2000, zoom=2.0, center=(-0.5, 0.0))
me.render_image("frame.ppm", 1920, 1080, max_iterations=2000, format="pnm")

自定义缓冲(零拷贝,可复用):

import numpy as np
colors = np.empty((720, m.storage_width, 3), dtype=np.uint8)   # storage_width 是按 32 对齐的宽度
iterations = np.empty((720, m.storage_width), dtype=np.int32)
m.render_into(colors, iterations)                              # 使用 colors[:, :m.width] 得到有效区域

视频导出(需要 OpenCV 版的 mandelbrot_explorer_video 库;找不到库抛 FileNotFoundError, 没有可用编码器抛 me.VideoError):

me.write_video("zoom.mp4", 1920, 1080, max_iterations=2000, threads=0,
                        fps=60, duration=10, codec="h264", quality=-1)
# 自定义视角:center 为中心点,start_zoom/end_zoom 为两端缩放(相等即静态视角)
me.write_video("custom.mp4", 1280, 720, fps=30, duration=5,
                        center=(-0.7436, 0.1318), start_zoom=1.0, end_zoom=1000.0)

不传 center/start_zoom/end_zoom 时使用与 CLI 相同的默认动画;每帧倍率由帧数推算, 最后一帧精确到达 end_zoom。

库的查找顺序

m.set_... 之前无需设置路径,绑定按以下顺序查找:

  1. mandelbrot.load_library("path") 或构造函数 / 函数参数 library=...
  2. 环境变量 MANDELBROT_EXPLORER_LIBRARY(视频库为 MANDELBROT_EXPLORER_VIDEO_LIBRARY)
  3. 包目录(与 __init__.py 同级)
  4. 仓库构建目录(build/*/Release/mandelbrot_explorer.dll、build/*/libmandelbrot_explorer.so 等)
  5. 系统搜索路径(mandelbrot_explorer.dll / libmandelbrot_explorer.so / libmandelbrot_explorer.dylib)

自测与示例

python3 bindings/python/selftest.py --library build/libmandelbrot_explorer.so
python3 bindings/python/examples/render_image.py --size 1280x720 --iterations out.bmp

selftest.py 需要 numpy;缺少 numpy 时以退出码 3 跳过(与 ctest 的 SKIP_RETURN_CODE 一致), cmake 找到 Python 解释器时会自动把它注册为 python_binding 测试。

Metadata

Release files for mandelbrot-explorer 0.6.0

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 mandelbrot-explorer 0.6.0
File Interpreter ABI Platform
mandelbrot_explorer-0.6.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details
mandelbrot_explorer-0.6.0-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl Python 3 none Linux glibc 2.28+ x86-64, Linux glibc 2.27+ x86-64 Details
mandelbrot_explorer-0.6.0-py3-none-macosx_14_0_arm64.whl Python 3 none macOS 14.0+ ARM64 Details

Total release size: 221.2 kB

Release files / mandelbrot_explorer-0.6.0-py3-none-win_amd64.whl

Download URL mandelbrot_explorer-0.6.0-py3-none-win_amd64.whl
Size 38.5 kB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
424e610b1257b3bfe3b5931470a2951853d216fbefbabd80fe4c3bfa4be616ed
BLAKE2b-256 checksum
How to use checksums
1f9cc6514e1ef2c1593b74fc936035b11b8f5280e2576d796c8917b5fb8a9d93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 30, 2026.

Transparency log

Release files / mandelbrot_explorer-0.6.0-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL mandelbrot_explorer-0.6.0-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 111.6 kB
Tags Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 Python 3
SHA-256 checksum
How to use checksums
1c00e9950802c4f21e199f927a3f8a7bc0ec7cd77d5d88a47735a4677b2f540a
BLAKE2b-256 checksum
How to use checksums
7e2c9b5ebf2c79b04ad7c53249923a3f14f18320f906f83243289f7df74efe3c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 30, 2026.

Transparency log

Release files / mandelbrot_explorer-0.6.0-py3-none-macosx_14_0_arm64.whl

Download URL mandelbrot_explorer-0.6.0-py3-none-macosx_14_0_arm64.whl
Size 71.2 kB
Tags Python 3 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
81c55df592b1620071e53ad540143377fb616ce55fc129a85d5e2a1fd018af69
BLAKE2b-256 checksum
How to use checksums
975df7ae74c83af8d20057ede04ebf87f79330beaccc625338c8c85c131f2717
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 30, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.6.0 This release

3 release files

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