resvg-py
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.
Warnings
- gotcha resvg-py inherits limitations from the underlying `resvg` Rust library regarding SVG feature support. It primarily supports a static subset of SVG 1.1/2, explicitly excluding dynamic features such as animations, scripting, or elements like `<a>`, `script`, `view`, or `cursor`. Users expecting full browser-like SVG rendering capabilities will find these features unsupported.
- gotcha The library does not use native system text rendering. It relies on its own internal font handling mechanisms, which might lead to inconsistencies if specific system fonts or exact rendering matches are expected. Users need to ensure necessary fonts are explicitly loaded if custom fonts are used in their SVGs.
- gotcha SVG files that specify dimensions in `mm` (millimeters) units might fail to load with an 'SVG has an invalid size' error.
Install
-
pip install resvg-py
Imports
- svg_to_bytes
from resvg_py import svg_to_bytes
Quickstart
import resvg_py
svg_string = """
<svg width="300" height="130" xmlns="http://www.w3.org/2000/svg">
<rect width="200" height="100" x="10" y="10" rx="20" ry="20" fill="blue" />
</svg>
"""
# Renders SVG to PNG bytes
png_bytes = resvg_py.svg_to_bytes(svg_string=svg_string)
# The 'png_bytes' variable now holds the binary data of the rendered PNG image.
# You can save it to a file or process it further.
print(f"Generated PNG bytes of length: {len(png_bytes)}")