Skip to main content

Thread-safe SVG → PNG rendering for Python, powered by resvg

Project description

svg2png-py

Fast, accurate SVG → PNG rendering for Python, powered by the resvg Rust library. Built with PyO3 and Maturin.

Unlike the original resvg-py, this binding is fully thread-safe: a FontDatabase instance can be shared across threads and called concurrently from a ThreadPoolExecutor without any locking on the Python side.


Features

  • Full-fidelity SVG → PNG rasterization via resvg / tiny-skia
  • Thread-safe FontDatabase — share one instance across a thread pool
  • Affine-free transforms — pass a plain 6-tuple, no extra dependencies
  • Batch rendering via render_many
  • Background canvas: solid colour, existing PNG file, or raw PNG bytes
  • Pre-built wheels for Windows, Linux, and macOS (Python ≥ 3.9)

Installation

pip install svg2png-py

Quick start

import svg2png_py

# Load fonts (system fonts + a custom one)
db = svg2png_py.FontDatabase.system()
db.load_font_file("/path/to/MyFont.ttf")

svg = open("diagram.svg", encoding="utf-8").read()

# Render with the SVG's intrinsic size
png_bytes = svg2png_py.svg_to_png(svg, db)
open("diagram.png", "wb").write(png_bytes)

API

FontDatabase

db = svg2png_py.FontDatabase()          # empty
db = svg2png_py.FontDatabase.system()   # pre-loaded with system fonts

db.load_system_fonts()                 # add system fonts to an existing db
db.load_font_file("/path/to/font.ttf") # load a single file
db.load_fonts_dir("/path/to/fonts/")   # load all fonts in a directory
len(db)                                # number of loaded font faces

RenderOptions

opts = svg2png_py.RenderOptions(
    dpi=96.0,                       # default: 96.0
    font_family="Helvetica",        # default: "Times New Roman"
    font_size=12.0,                 # default: 12.0
    resources_dir="/path/to/svgs/", # default: None (cwd)
)

svg_to_png

png: bytes = svg2png_py.svg_to_png(
    svg_str,            # str — SVG content
    font_db,            # FontDatabase
    transform=None,     # 6-tuple (a,b,c,d,e,f) or None for identity
    bg_file=None,       # str — path to a PNG background
    bg_data=None,       # bytes — raw PNG background
    bg_size=None,       # (w, h) — canvas size (default: SVG intrinsic)
    bg_color=None,      # (r, g, b, a) — fill colour 0-255
    options=None,       # RenderOptions
)

render_many

pages: list[bytes] = svg2png_py.render_many(
    svg_strings,        # list[str]
    font_db,            # FontDatabase
    transform=None,     # shared transform for all pages
    bg_color=None,      # shared fill colour for all pages
    options=None,       # shared RenderOptions
)

Transforms

The transform parameter accepts a 6-tuple (a, b, c, d, e, f) in row-major order. This is the same layout as affine.Affine(...)[0:6], so code that previously depended on the affine package can simply pass the slice directly — no library change needed:

# Before (affine required):
# from affine import Affine
# tr = Affine.scale(2)
# data = old_render(tree, tr[0:6])

# After (no extra dependency):
a, b, c, d, e, f = 2, 0, 0, 0, 2, 0   # scale ×2
png = svg2png_py.svg_to_png(svg, db, transform=(a, b, c, d, e, f))

Pass None (the default) for the identity transform.


Thread-pool example

import svg2png_py
from concurrent.futures import ThreadPoolExecutor

db = svg2png_py.FontDatabase.system()
svgs = [open(f"page{i}.svg").read() for i in range(20)]

def render_one(svg):
    return svg2png_py.svg_to_png(svg, db)   # db shared across threads ✓

with ThreadPoolExecutor() as pool:
    pngs = list(pool.map(render_one, svgs))

Credits

This package is a rewrite of resvg-py by Brice Yan, used under the MIT licence.

It wraps the resvg crate (formerly by RazrFalcon, now maintained by the Linebender project), which uses tiny-skia for rasterization and fontdb for font resolution.


License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

svg2png_py-1.1.0-cp311-abi3-win_amd64.whl (1.5 MB view details)

Uploaded CPython 3.11+Windows x86-64

svg2png_py-1.1.0-cp311-abi3-manylinux_2_34_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.34+ x86-64

svg2png_py-1.1.0-cp311-abi3-macosx_11_0_arm64.whl (1.4 MB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

File details

Details for the file svg2png_py-1.1.0-cp311-abi3-win_amd64.whl.

File metadata

  • Download URL: svg2png_py-1.1.0-cp311-abi3-win_amd64.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: CPython 3.11+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for svg2png_py-1.1.0-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 6bef5607bbc64003b350c8e0f7a6a20725d5226009f1666ddd4e2a1106cb0f54
MD5 e4802d3a911d91fd32e6c4bc801cb45e
BLAKE2b-256 d22038dfaaef3aa1515970931d423965afe50bc6bc8671e9bf292825070ac52b

See more details on using hashes here.

Provenance

The following attestation bundles were made for svg2png_py-1.1.0-cp311-abi3-win_amd64.whl:

Publisher: release.yml on IFSCM/svg2png-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file svg2png_py-1.1.0-cp311-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for svg2png_py-1.1.0-cp311-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 7a7af6e83d09f7eaeae2b1ee541e6d7e5271fe4028a1008e37fd8985da9001a0
MD5 732ac7b365785e0d9d24c73b949b90c3
BLAKE2b-256 6d4e87db36ccf8f4015295c694ce9fc6adcd43754d515a0ba3fd54e2b3107af8

See more details on using hashes here.

Provenance

The following attestation bundles were made for svg2png_py-1.1.0-cp311-abi3-manylinux_2_34_x86_64.whl:

Publisher: release.yml on IFSCM/svg2png-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file svg2png_py-1.1.0-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for svg2png_py-1.1.0-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fa748d733e20954d18b0b6b8000900a5a9858443f109cb654ea87b1c3525e214
MD5 08cce3a9dc650fe1393dffc1b01e1276
BLAKE2b-256 1e9b9a367da269e90047f4d727e21ddd9df23ad734643382a1e6b7154c61d96e

See more details on using hashes here.

Provenance

The following attestation bundles were made for svg2png_py-1.1.0-cp311-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on IFSCM/svg2png-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page