Resvg-py is a Python package that provides high-performance SVG rendering by wrapping the `resvg` Rust library using PyO3. This package allows Python applications to easily render SVG files to various image formats such as PNG, PDF, and SVGZ, with high fidelity and performance. It aims to offer safe and high-level bindings to the underlying `resvg` project. The library maintains a frequent release cadence, with updates often occurring monthly or bi-monthly.
pip install resvg-pyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to convert a simple SVG string into PNG image bytes using `resvg-py`'s `svg_to_bytes` function.
Ensure your SVGs adhere to a static subset of the specification. Avoid dynamic features or elements if rendering with `resvg-py`.
For custom fonts, use the appropriate API (e.g., `usvg.FontDatabase.default().load_system_fonts()` or similar methods if exposed by `resvg_py`) to load them for consistent rendering. Refer to the documentation for advanced font handling.
To resolve this, modify the SVG file to use pixel (`px`) units for dimensions, or simply remove the 'mm' suffix from the `width` and `height` attributes, allowing `resvg-py` to parse them correctly.
Install the Rust toolchain using rustup: `curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh` and ensure it's added to your system's PATH.
Provide either the `output_path` argument to save to a file, or the `fmt` argument to return the image data as bytes, for example: `resvg(svg_data, output_path='output.png')` or `png_data = resvg(svg_data, fmt='png')`.
Install the package using pip: `pip install resvg-py` or ensure you are working within the correct virtual environment where the package is installed.
Use the correct function name `resvg`: `from resvg_py import resvg; resvg(svg_data, output_path='output.png')` or `import resvg_py; resvg_py.resvg(svg_data, output_path='output.png')`.
Install the Rust toolchain (`curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh`) and ensure your system has C/C++ build tools (e.g., `build-essential` on Linux, Xcode Command Line Tools on macOS), then retry `pip install resvg-py`.