Bindings for lunasvg
Project description
pylunasvg
pylunasvg provides Python bindings for LunaSVG, an SVG rendering library in C++, designed to be lightweight and portable, offering efficient rendering and manipulation of Scalable Vector Graphics (SVG) files. The bindings are built with pybind11, and the entire public API of LunaSVG is available in Python.
If you have any questions or problems, feel free to open an issue!
Basic Usage
import pylunasvg as pl
document = pl.Document.loadFromFile("tiger.svg")
if document is None:
raise SystemExit("failed to load document")
bitmap = document.renderToBitmap()
if bitmap.isNull():
raise SystemExit("failed to render")
bitmap.writeToPng("tiger.png")
You can also render directly into a NumPy array without touching disk:
import pylunasvg as pl
import numpy as np
document = pl.Document.loadFromFile("tiger.svg")
bitmap = document.renderToBitmap()
bitmap.convertToRGBA()
arr = np.array(bitmap, copy=False) # zero-copy view of the pixel data
# Do something useful
Dynamic Styling
import pylunasvg as pl
landscape_content = """
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 600" width="800" height="600">
<!-- Background (Sky) -->
<rect width="800" height="600" class="sky"/>
<!-- Sun -->
<circle cx="650" cy="150" r="80" class="sun" />
<!-- Clouds -->
<ellipse cx="200" cy="150" rx="100" ry="40" class="cloud" />
<ellipse cx="250" cy="200" rx="120" ry="50" class="cloud" />
<ellipse cx="500" cy="80" rx="150" ry="60" class="cloud" />
<ellipse cx="550" cy="120" rx="120" ry="50" class="cloud" />
<!-- Mountains -->
<polygon points="0,450 200,200 400,450" class="mountain" />
<polygon points="200,450 400,100 600,450" class="mountain" />
<polygon points="400,450 600,250 800,450" class="mountain" />
<!-- Foreground (Ground) -->
<rect y="450" width="800" height="150" class="ground" />
</svg>
"""
summer_style = """
.sky { fill: #4A90E2; }
.sun { fill: #FF7F00; }
.mountain { fill: #2E3A59; }
.cloud { fill: #FFFFFF; opacity: 0.8; }
.ground { fill: #2E8B57; }
"""
winter_style = """
.sky { fill: #87CEEB; }
.sun { fill: #ADD8E6; }
.mountain { fill: #2F4F4F; }
.cloud { fill: #FFFFFF; opacity: 0.8; }
.ground { fill: #FFFAFA; }
"""
document = pl.Document.loadFromData(landscape_content)
document.applyStyleSheet(summer_style)
document.renderToBitmap().writeToPng("summer.png")
document.applyStyleSheet(winter_style)
document.renderToBitmap().writeToPng("winter.png")
summer.png |
winter.png |
|---|---|
Hit Testing
This example demonstrates SVG element hit testing using elementFromPoint(x, y). It loads an SVG containing three shapes, performs point-based hit detection, and applies a skew transform with a black stroke to each matched element. The results are saved as original.png and modified.png for visual comparison.
import pylunasvg as pl
svg_content = """
<svg width="400" height="200" xmlns="http://www.w3.org/2000/svg">
<rect id="red-rect" x="20" y="20" width="100" height="100" fill="red"/>
<circle id="blue-circle" cx="200" cy="70" r="50" fill="blue"/>
<rect id="green-rect" x="300" y="30" width="70" height="130" fill="green"/>
</svg>
"""
document = pl.Document.loadFromData(svg_content)
document.renderToBitmap().writeToPng("original.png")
points = [
(30, 30), # inside red-rect
(200, 70), # center of blue-circle
(310, 50), # inside green-rect
(0, 0), # outside all shapes
]
for x, y in points:
element = document.elementFromPoint(x, y)
if not element.isNull():
print(f"Element at ({x}, {y}): {element.getAttribute('id')}")
element.setAttribute("stroke", "black")
element.setAttribute("stroke-width", "3")
element.setAttribute("transform", "skewX(9)")
else:
print(f"No element found at ({x}, {y})")
document.renderToBitmap().writeToPng("modified.png")
original.png |
modified.png |
|---|---|
Element at (30, 30): red-rect
Element at (200, 70): blue-circle
Element at (310, 50): green-rect
No element found at (0, 0)
Features
LunaSVG supports nearly all graphical features outlined in the SVG 1.1 and SVG 1.2 Tiny specifications. The primary exceptions are animation, filters, and scripts. As LunaSVG is designed for static rendering, animation is unlikely to be supported in the future. However, support for filters may be added. It currently handles a wide variety of elements, including:
<a> <circle> <clipPath> <defs> <ellipse> <g> <image> <line> <linearGradient> <marker> <mask> <path> <pattern> <polygon> <polyline> <radialGradient> <rect> <stop> <style> <svg> <symbol> <text> <tspan> <use>
Installation
pip install pylunasvg
Prebuilt wheels are published for common platforms. If a wheel is not available for yours, pip will build from source (see below).
Building From Source
-
Clone this repository with the
--recursiveflag. This is needed because the project uses LunaSVG and pybind11 as git submodules:git clone --recursive https://github.com/erentknn/pylunasvg.git cd pylunasvg
-
Install from the repository directory. This builds LunaSVG and then pylunasvg:
pip install .
After this completes you can try the examples above. If you run into any errors, please submit an issue.
Demo
pylunasvg ships with a command-line tool for converting SVG files to PNG. Unless otherwise specified, the output is saved next to the SVG file.
Usage:
pylunasvg [filename] [options]
Examples:
pylunasvg input.svg # input.png next to the source
pylunasvg input.svg -W 512 -H 512 # explicit output size
pylunasvg input.svg -s 2.0 -o out.png # 2x scale, custom output path
| Option | Description |
|---|---|
-W, --width |
Width of the output in pixels |
-H, --height |
Height of the output in pixels |
-s, --scale |
Scale factor applied to the output |
-o, --output |
Output file name (defaults to the input name with .png) |
License
pylunasvg is licensed under the MIT License, see LICENSE for more information.
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 Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pylunasvg-0.2.0.tar.gz.
File metadata
- Download URL: pylunasvg-0.2.0.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d79e4cfd290459fd8c2e894819f953d31a0284504fd793f8aaddd0a3a7e4e49
|
|
| MD5 |
4a80110b27a643ab772e979675966179
|
|
| BLAKE2b-256 |
79fcd4453d999fcf88b3b44a576f6adb103523e73deab6f41a86c8d41f9f8c49
|
File details
Details for the file pylunasvg-0.2.0-cp314-cp314-win_amd64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp314-cp314-win_amd64.whl
- Upload date:
- Size: 512.8 kB
- Tags: CPython 3.14, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1bf00060ce3c9c383615db2f981b260ba91ac22fea4c5f4f4cf86889c33d9ef8
|
|
| MD5 |
44515ca3c5e5537faccc481319508bc6
|
|
| BLAKE2b-256 |
acd5cd1ba868cf6095f067c947ade6913c5005363e29a02be354b98124480d40
|
File details
Details for the file pylunasvg-0.2.0-cp314-cp314-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp314-cp314-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 437.6 kB
- Tags: CPython 3.14, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30f3cd8659e5b24c195f21a8cc0f4ba314c59be306d83ce7d6d676d12128c9af
|
|
| MD5 |
7c20ca7226734bfeb7b8a5ffb5c4aab3
|
|
| BLAKE2b-256 |
07a4fa7e0a2c41a13d1614b6a6c347e7c241e4aeb69f76c74a93161c943d07cc
|
File details
Details for the file pylunasvg-0.2.0-cp314-cp314-macosx_11_0_arm64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp314-cp314-macosx_11_0_arm64.whl
- Upload date:
- Size: 294.1 kB
- Tags: CPython 3.14, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13e61ebf2e2791058a39635faebb20a5eeb7158eb766bd69009dc07ea1cdab16
|
|
| MD5 |
ae06b4746c01c7a57e6ced825fefa60e
|
|
| BLAKE2b-256 |
0dd840023d726c50a5c75dd27613171695c7bedaff3f2a377401a7da8915a879
|
File details
Details for the file pylunasvg-0.2.0-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 495.6 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54923e3ee05282309dd1f37680bca85396450c926f40a004e7e3b78ebf87deeb
|
|
| MD5 |
89887f4f169bb1d11a94d9fe7f165e34
|
|
| BLAKE2b-256 |
522bca102f50569a5d39e17e4a920231d911b5cd9671684386df751dc639c709
|
File details
Details for the file pylunasvg-0.2.0-cp313-cp313-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp313-cp313-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 437.5 kB
- Tags: CPython 3.13, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b720d0e014f1d6af610b3181efe0befa6a04a7d96c344406a4686e2368a8696f
|
|
| MD5 |
2aef921ed53f731a56b40d9cf3d6a06c
|
|
| BLAKE2b-256 |
3e10023fd191cea4437a35fb22573a89c2e9264f812d6155e06d2ec02c39a65c
|
File details
Details for the file pylunasvg-0.2.0-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 293.7 kB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f7611308658819c3b294c40c4897e3a06596430e6ec831175685649be0bf901
|
|
| MD5 |
e726718e4a2b2a6e22294dab05f8b894
|
|
| BLAKE2b-256 |
ff7849f627e7dddb37422978fc7d49015e093a150a00430170c228ae1777277a
|
File details
Details for the file pylunasvg-0.2.0-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 495.6 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2615e3384634b8e4a52621ba95be89bb170870754da367cb35ac1f3cd154d139
|
|
| MD5 |
d8fcadebd78932f50e4d8c3546296584
|
|
| BLAKE2b-256 |
c0fdfc3727ca45178a2dea9a19006f7319aed5c59fa78299350ed54694550b25
|
File details
Details for the file pylunasvg-0.2.0-cp312-cp312-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp312-cp312-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 437.7 kB
- Tags: CPython 3.12, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7dea679189aef34e581dd3bb29c134b4b3e441cf90b8444fcd5386a2d68903d0
|
|
| MD5 |
c44c94a4423c65c917a971b5b7562729
|
|
| BLAKE2b-256 |
9da9fd96a9e46d078f94d0a1bfefb345c32b801c0a1fe5bdc18a20bbf5f1e2f1
|
File details
Details for the file pylunasvg-0.2.0-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 293.6 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d56516f4977ae261afb4000750fea9a63182bd1275e782c2072554df73b858f
|
|
| MD5 |
daa66a3dfab3ecf20e51a01fb158d11d
|
|
| BLAKE2b-256 |
a6487b2d99ec73a7b93bf576cc5a9c95856bc510a7db9fd640dc2cc52f2f082a
|
File details
Details for the file pylunasvg-0.2.0-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 495.1 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d2658c7acd170c604c34b6b871c33993d9f53741de3d3da2ef962062e2f906c
|
|
| MD5 |
f196f06664e2012ad0953f230ab32c3b
|
|
| BLAKE2b-256 |
00fc2e1d413e2bc17a5dab06492ae84cb1d359219c4ca04dc1f83c2a3afa9c69
|
File details
Details for the file pylunasvg-0.2.0-cp311-cp311-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp311-cp311-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 436.6 kB
- Tags: CPython 3.11, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
840e11843b3c253928004d188472cba2fe1daeb6a3790d8e1b3dc5e531990938
|
|
| MD5 |
14e439f227400c2e0a13997ed6a3f04f
|
|
| BLAKE2b-256 |
3e7c6f20dd87aa9ee6bd57fd7b3f2fb7d2ac8b708f5c786c3be5c5a7a5645163
|
File details
Details for the file pylunasvg-0.2.0-cp311-cp311-macosx_11_0_arm64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 293.1 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf40b35607a486a674305f284c203b430fd3a4e4dd50ac7beb024fc533468914
|
|
| MD5 |
1621f0fe6935de58c180c817f2a80dcf
|
|
| BLAKE2b-256 |
35b919c8c783b95419f48f66aca9c421f95f0c3faa8ceed79b6ce64c743d597b
|
File details
Details for the file pylunasvg-0.2.0-cp310-cp310-win_amd64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 494.3 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d99cb6478dc3f40d10d3916fb2ba07ee7e588397981d15c9dae4bf96173f052a
|
|
| MD5 |
fe43764c7fd3f49eae9bff54e0ab680c
|
|
| BLAKE2b-256 |
1605b0f1acca09db4cce0ba069595ac11c676e8ef395ffd62d5b61bae114795e
|
File details
Details for the file pylunasvg-0.2.0-cp310-cp310-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp310-cp310-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 435.8 kB
- Tags: CPython 3.10, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e77cb543c4c35d69293d3b5b3f6f161060c7cf83adb14941f68f64ba442c408c
|
|
| MD5 |
3689c8c0325b0172c082b13b9a832b64
|
|
| BLAKE2b-256 |
1f6d93e80ef67b3a3e1dfb2f897ce92442ad81c4f5d1eb8e00032c76590bc813
|
File details
Details for the file pylunasvg-0.2.0-cp310-cp310-macosx_11_0_arm64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 292.0 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1fe896cbdc1da0d1a578fdd53e9fc5d29972ed9e1b7bcc6e5d8d9f5e98405452
|
|
| MD5 |
ea8892017db9e49debac2840094886c7
|
|
| BLAKE2b-256 |
b967233330e90978754061fae82763b55457bce1a10048811d6f104007212c21
|
File details
Details for the file pylunasvg-0.2.0-cp39-cp39-win_amd64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 502.7 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
988292b3643fc5aac9f08b945d6abb2a77b1eb6b3f6e1551a00d8d1633a8d120
|
|
| MD5 |
daece1b157abb2e60e2e7924aa33d170
|
|
| BLAKE2b-256 |
0da55f0125c2a30448fbdb1c273d25517337e1fc86fb04a98bcc07463f64d805
|
File details
Details for the file pylunasvg-0.2.0-cp39-cp39-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp39-cp39-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 436.0 kB
- Tags: CPython 3.9, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a57d215fc2766ce5ebed7422e8b8de68575b9afe2cc59d10a8af74233ae8f94
|
|
| MD5 |
06ac25adaef7cb9d8007ccfc2145d32d
|
|
| BLAKE2b-256 |
7414e4bf35557e0115d485f225dd8a7fa1cb56646aca57e7e7cdd1db51941d09
|
File details
Details for the file pylunasvg-0.2.0-cp39-cp39-macosx_11_0_arm64.whl.
File metadata
- Download URL: pylunasvg-0.2.0-cp39-cp39-macosx_11_0_arm64.whl
- Upload date:
- Size: 292.1 kB
- Tags: CPython 3.9, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c76aaaa0e84087d20c0e5c5b8e58b57e93f5929be4901378e3a6df1112b0e769
|
|
| MD5 |
68e6f519445b742b85f033b3f0ae1d19
|
|
| BLAKE2b-256 |
c6b900f5207d0d402d0e8995cfb6911350072e4973ec09d704e2f0989c2c2f3d
|