A manipulation and optimization library for Scalable Vector Graphics
Project description
svglab
A manipulation and optimization library for Scalable Vector Graphics
Table of Contents
About The Project
Features
- SVG parsing, manipulation, and writing
- Support for all SVG 1.1 elements and attributes
- Partial support for SVG 2
- Support for special XML entities (
CDATA, comments, text) - Attributes are parsed into native Python types for easy manipulation
- Highly configurable formatting options:
- indentation level
- maximum precision for floating-point numbers
- color mode (
rgb,rgba,hsl,hex,named) - relative/absolute path commands
- scientific notation for small/large numbers
- and many more...
- Strong type safety:
- one class per distinct SVG element
- typed attributes
- runtime validation thanks to pydantic
- Support for all beautifulsoup4 parsers (e.g.,
html.parser,lxml,html5lib) - SVG can be rendered into a raster image using resvg
- Support for calculating the bounding box and mask of an element
- Support for applying transformations in the
transformattribute ("reification")
---
title: Entity hierarchy
---
graph TD
Entity:::abc --> CharacterData
Entity --> Element
Element:::abc --> G
Element --> Svg
Element --> Rect
Element --> Circle
Element --> etc1[...]
CharacterData:::abc --> RawText
CharacterData --> Comment
CharacterData --> CData
etc1:::etc
classDef abc stroke:white,stroke-width:2px;
classDef etc stroke:gray,stroke-width:2px;
classDef default stroke:orange,stroke-width:2px;
Getting Started
Prerequisites
Installation
From PyPi:
pip install svglab
From source:
# Via HTTPS
pip install git+https://github.com/reznakt/svglab.git
# Via SSH
pip install git+ssh://git@github.com/reznakt/svglab.git
Usage
# Parse an existing SVG file
svg = parse_svg(
"""
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
<g>
<rect
id="background"
width="100cm"
height="100%"
transform="rotate(45)"
stroke="red"
/>
<rect color="hsl(0, 100%, 100%)"/>
<!-- This is a comment -->
<![CDATA[.background { fill: blue; }]]>
Hello SVG!
<path d="M 10,10 H 10 L 100,100 Q 100,100 50,50 v 100 Z"/>
<path d="M0,0 10,10 20,20 S 100,100 50,50 t 100,100 M 50,50 z"/>
<path d="M0,0A50,50 90 1 0 100,100v100h-10z"/>
<polygon points="0,0 100,0 100,100 0,100"/>
</g>
</svg>
"""
)
print(svg)
# Create an element programmatically
group = G().add_children(
Rect(
width=Length(15, "px"),
height=Length(20),
transform=[SkewX(45.123), Translate(10, 20)],
color=Color("#ff0000"),
),
Comment("This is a comment"),
CData(".background { fill: blue; }"),
RawText("Hello SVG!"),
Path(
d=PathData()
.move_to(Point(10, 10))
.line_to(Point(100, 100))
.quadratic_bezier_to(Point(100, 100), Point(50, 50))
.smooth_quadratic_bezier_to(Point(100, 100))
.move_to(Point(50, 50))
.cubic_bezier_to(
Point(100, 100), Point(100, 100), Point(10, 10)
)
.smooth_cubic_bezier_to(Point(100, 100), Point(50, 50))
.arc_to(
Point(50, 50), 90, Point(100, 100), large=True, sweep=False
)
.vertical_line_to(100)
.horizontal_line_to(-10, relative=True)
.close()
),
Polyline(
points=[
Point(0, 0),
Point(100, 0),
Point(100, 100),
Point(0, 100),
],
stroke_linecap="square",
opacity=0.5,
),
)
# Add the element to the SVG
svg.add_child(group)
# Manipulate attributes
print(svg.xmlns) # http://www.w3.org/2000/svg
svg.x = Length(10, "px")
# Save to a file
svg.save(sys.stdout)
# Search the entity tree
print(*svg.find_all(Rect), sep="\n")
rect = svg.find(G).find(Rect)
# Compute the bounding box and mask of an element
print(rect.get_bbox())
print(rect.get_mask())
# Render the SVG to an image
image = svg.render()
print(image)
# Apply transformations in the transform attribute
svg.reify()
print(svg.to_xml())
# Change the view box
svg.set_viewbox((0, 0, 50, 50))
print(svg.to_xml())
Development
Setup
# Install dependencies
uv sync
# Activate the virtual environment
source .venv/bin/activate
# Optional: Install pre-commit hooks
pre-commit install
Common tasks
# Run tests
just test
# Run type checker
just typecheck
# Run linter
just lint
# Fix linting errors
just lint-fix
# Run formatter
just format
# Fix formatting errors
just format-fix
License
This software is distributed 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 Distribution
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 svglab-0.15.0.tar.gz.
File metadata
- Download URL: svglab-0.15.0.tar.gz
- Upload date:
- Size: 226.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81959c444e34abbefdecc5d4f777c3bc429de641c3630fd3779612eb9fb26a8d
|
|
| MD5 |
11c5be1c3322ccdb9ec42dede78ec419
|
|
| BLAKE2b-256 |
e268570e32a930f4e949a5e511086e85bfebd16f3a61026f54cd72be13a95461
|
Provenance
The following attestation bundles were made for svglab-0.15.0.tar.gz:
Publisher:
publish-pypi.yml on reznakt/svglab
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
svglab-0.15.0.tar.gz -
Subject digest:
81959c444e34abbefdecc5d4f777c3bc429de641c3630fd3779612eb9fb26a8d - Sigstore transparency entry: 1441630455
- Sigstore integration time:
-
Permalink:
reznakt/svglab@c47dec3b0fec753a55e7f2f5e7c051c89f97a8c6 -
Branch / Tag:
refs/tags/v0.15.0 - Owner: https://github.com/reznakt
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@c47dec3b0fec753a55e7f2f5e7c051c89f97a8c6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file svglab-0.15.0-py3-none-any.whl.
File metadata
- Download URL: svglab-0.15.0-py3-none-any.whl
- Upload date:
- Size: 94.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3da6952784cf5f72ea3b60e0d9ad65cc0a7067801794d4d315182cae43a625c4
|
|
| MD5 |
629aaf3bd04a582d4eba8f5a2e5468bb
|
|
| BLAKE2b-256 |
3773193b1ed1e1c665c6f3e102c6dc00f0e3b89119bc4445aacbac14d2723a81
|
Provenance
The following attestation bundles were made for svglab-0.15.0-py3-none-any.whl:
Publisher:
publish-pypi.yml on reznakt/svglab
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
svglab-0.15.0-py3-none-any.whl -
Subject digest:
3da6952784cf5f72ea3b60e0d9ad65cc0a7067801794d4d315182cae43a625c4 - Sigstore transparency entry: 1441630541
- Sigstore integration time:
-
Permalink:
reznakt/svglab@c47dec3b0fec753a55e7f2f5e7c051c89f97a8c6 -
Branch / Tag:
refs/tags/v0.15.0 - Owner: https://github.com/reznakt
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@c47dec3b0fec753a55e7f2f5e7c051c89f97a8c6 -
Trigger Event:
release
-
Statement type: