Python bindings for the Blend2D rendering engine using nanobind.
This project has been converted from the original Cython-based implementation to use nanobind, which offers:
- Improved performance
- Better compatibility with modern C++
- Simplified binding code
- Cleaner implementation
- C++17 compatible compiler
- CMake 3.15 or newer
- Python 3.10 or newer
- NumPy
Pre-built wheels are available for:
- Windows (x86_64)
- macOS (x86_64, arm64/Apple Silicon)
- Linux (x86_64, aarch64/ARM64)
- WebAssembly (via Emscripten)
pip install blend2d-pythonpip install .Or for development:
pip install -e .import numpy as np
import blend2d
# Create an image
img = blend2d.Image(480, 480, blend2d.Format.PRGB32)
# Attach a rendering context to it
ctx = blend2d.Context(img)
# Clear the image with white color
ctx.fill_all((1.0, 1.0, 1.0, 1.0))
# Create a linear gradient
gradient = blend2d.create_linear_gradient(0, 0, 480, 480)
gradient.add_stop(0.0, (1.0, 0.0, 0.0, 1.0))
gradient.add_stop(0.5, (0.0, 1.0, 0.0, 1.0))
gradient.add_stop(1.0, (0.0, 0.0, 1.0, 1.0))
# Create a path (a star, for example)
path = blend2d.Path()
path.move_to(240, 80)
for i in range(5):
angle = -(2 * i * 2.0 * np.pi / 5.0 + np.pi / 2)
path.line_to(240 + 160 * np.cos(angle), 240 + 160 * np.sin(angle))
# Fill the path with the gradient
ctx.set_fill_style(gradient)
ctx.fill_path(path)
# Save the image to a file
img.write_to_file("star.png")This project uses cibuildwheel to build wheels for multiple platforms:
- Windows (x86_64)
- macOS (x86_64, arm64/Apple Silicon)
- Linux (x86_64, aarch64/ARM64)
- WebAssembly (via Emscripten)
To build wheels locally:
pip install cibuildwheel
python -m cibuildwheel --platform autoMIT License
This project was originally created by John Wiggins using Cython, and has been converted to use nanobind.