Library for loading SVG paths into shapely objects.
Project description
Library for Loading SVG Paths into Shapely Objects
This library provides some utility functions to find all paths in a SVG document and create shapely geometry objects from them.
It uses svg.path, shapely and numpy under the hood.
Installation
The package is installable via PyPI.
Basic Usage
The intended workflow is as follows.
First, load a SVG document and use the find_all_paths_in_svg method query the element tree for path elements featuring a d attribute.
The optional argument with_namespace determines, whether the SVG namespace shall respected in the query (if false, all path elements no matter what namespace are found).
As the first argument you may give a string containing SVG code (which will be parsed by xml.etree.ElementTree), or already parsed xml.etree.ElementTree.ElementTree/xml.etree.ElementTree.Element instances.
Alternatively you may read directly from file with find_all_paths_in_file.
from svg_path_to_shapely import find_all_paths_in_svg
paths = find_all_paths_in_svg("your svg code...", with_namespace=True)
or
from svg_path_to_shapely import find_all_paths_in_svg
from xml.etree.ElementTree import parse
et = parse("some path-like...")
paths = find_all_paths_in_svg(et, with_namespace=True)
or
from svg_path_to_shapely import find_all_paths_in_svg
from xml.etree.ElementTree import fromstring
et = fromstring("your svg code...")
paths = find_all_paths_in_svg("your svg code...", with_namespace=True)
or
from svg_path_to_shapely import find_all_paths_in_file
paths = find_all_paths_in_file("some path-like...", with_namespace=True)
paths will then be a list of xml.etree.ElementTree.Element instances representing all path element in the document.
Then, create svg.path.Path instances from those by use of parse_path.
This step is intentionally left explicit to be able to query for additional attributes as needed (such as id) on the path elements.
You may supply the element instance directly or a string with a valid value of the d attribute.
from svg_path_to_shapely import parse_path
parsed = [parse_path(p) for p in paths]
Last, convert those path instances to shapely geometries using convert_path_to_line_string.
This function may return a LineString, LinearRing or MultiLineString, depending on whether the path is open, closed or multi-part (with multiple M/m directives), respectively.
The optional parameter count determines the number of evenly spaced discrete points to approximate arcs and Bezier curves with (as shapely does only know linear strings).
from svg_path_to_shapely import convert_path_to_line_string
geoms = [convert_path_to_line_string(p, count=11) for p in parsed]
The latter will check if the path is multi-part and split it accordingly.
To spare this effort, if you know the path is single-part, you may use convert_single_part_path_to_line_string instead.
This will essentially treat multiple move directives (M/m) as if they were line directives (L/l).
The library exports some more of its lower-level functions. Have a look into their docstrings for information on how to use them.
Application Examples
You find example SVG documents and respective code in the test directory.
Powder Particle Shape Analysis
A micrograph of copper powder particles was imported in Inkscape and paths were manually drawn around the particles to determine their contours.
The paths were extracted and converted to shapely geometries to be able to analyse their geometric properties further (for the sake of example just centered at (0, 0) and plotted again).
Building and Testing
Project dependencies and build process are maintained using uv.
Build the package using uv build.
Test are run using uv run pytest.
License
The software is distributed under the terms of the MIT License.
Contributing
Issues and pull requests are welcome without any special contribution guidelines.
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 svg_path_to_shapely-0.1.0.tar.gz.
File metadata
- Download URL: svg_path_to_shapely-0.1.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Manjaro Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2604252754b4af9df7b1bedd2da0c41f2c57f11fb6f8e5babfa59b77c6ad5f07
|
|
| MD5 |
722308d7d9eefba3d1ad1a54fdf4fa78
|
|
| BLAKE2b-256 |
9d2fd72c9df5a97e8136c1d99c99dc1ec2bfd5a7bc5adb057ca572f3b59a9dec
|
File details
Details for the file svg_path_to_shapely-0.1.0-py3-none-any.whl.
File metadata
- Download URL: svg_path_to_shapely-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Manjaro Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59483b3279e62c0012ef4b5efeba14a2d067f40c3eb142e09b8b2f6f71bc1d92
|
|
| MD5 |
33ece8f45fa72cd0d6dad73c04e24221
|
|
| BLAKE2b-256 |
2bb17eb7a061bc40408d75a66ba56c0ca14be92d4a202e1ab410ff086f449b95
|