A Python binding of resvg, the high-performance SVG rendering library in Rust
Project description
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 with high fidelity and performance.
See https://github.com/RazrFalcon/resvg for more info about resvg.
This project is inspired by https://github.com/yisibl/resvg-js. The image displayed above has been modified from a test image(SVG) included in this project and was converted using this package.
Installation
pip install resvg affine
Usage
Here's a simple example of how to use resvg-py to convert an SVG file to a PNG image:
from resvg import render, usvg
import affine
with open("a.svg", "r") as f:
svg = f.read()
db = usvg.FontDatabase.default()
db.load_system_fonts()
options = usvg.Options.default()
options.font_family = "Space Mono"
tree = usvg.Tree.from_str(svg, options, db)
tr = Affine.identity()
data = render(tree, tr[0:6])
with open("a.png", "wb") as out:
out.write(bytes(data))
File content of a.svg
<svg width="600" height="300" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<linearGradient x1="0%" y1="45%" x2="100%" y2="55%" id="b">
<stop stop-color="#FF8253" offset="0%" />
<stop stop-color="#DA1BC6" offset="100%" />
</linearGradient>
<path id="a" d="M0 0h600v300H0z" />
</defs>
<g fill="none" fill-rule="evenodd">
<use fill="url(#b)" xlink:href="#a" />
<text x="50%" y="20%" font-size="48" fill="#FFF" dominant-baseline="middle" text-anchor="middle">
resvg-py</text>
<text x="50%" y="40%" font-size="24" fill="#FFF" dominant-baseline="middle" text-anchor="middle">
a Python binding of resvg</text>
<text x="50%" y="60%" font-size="48" fill="#FFF" dominant-baseline="middle" text-anchor="middle">
resvg</text>
<text x="50%" y="80%" font-size="24" fill="#FFF" dominant-baseline="middle" text-anchor="middle">
an SVG rendering library in Rust
</text>
</g>
</svg>
Affine Transformation
The resvg-py package utilizes the affine library for the convenient creation of affine transformation matrices, thereby eliminating the need to directly wrap the Transformation class from tiny_skia.
It's important to note that when providing a 6-element tuple as the second parameter to the render()
function, this tuple should be in row-major format. This detail is crucial for those who might calculate the transformation matrix using other methods or libraries before passing it to resvg-py.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file resvg-0.1.2.tar.gz
.
File metadata
- Download URL: resvg-0.1.2.tar.gz
- Upload date:
- Size: 60.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe0cb560ac47318c6edd85ad1ca04be2ce2649a9847dae23ed42f83a7a8546cd |
|
MD5 | 0c5323ec3595e40e1cf918fca3ffed0c |
|
BLAKE2b-256 | 9bd9cc9675cfee6c86d1f953c039180c88802e597927608145fa562c09759f8e |
File details
Details for the file resvg-0.1.2-cp310-cp310-macosx_11_0_arm64.whl
.
File metadata
- Download URL: resvg-0.1.2-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 425570ee6365c92d53a39e37ce4422a600a13375173c3fca4c098bcb64f410fe |
|
MD5 | ef1dea0f79b5d0350e35c0ba855591c0 |
|
BLAKE2b-256 | a1f9506a90e6d16d74d10e11390dfd1939e169542cb047e0ea12d6785cc5d9d4 |