svg_path_data
Convert floats to svg-readable strings. Convert between svg path d strings and non-rational Bézier control points.
format floats
format_number converts a float or float string to the shortest svg-readable float representation. Uses the shorter of fixed-point or exponential notation.
format_number("5000")
# "5e3"
format_number("-17e+06")
# "-1.7e7"
format_number(2/3)
# ".6666666666666666"
format_number(-1/10e6)
# "-1e-7"
An optional but recommended resolution argument limits the resolution of the output. The default None limits information loss when converting back and forth between float and string. The resolution that makes sense for your svg will depend on the scale of your svg. Less than 1/100000-th of a unit won't be visible on any browser, and even a 600 dpi giclée print will have less that 22k dots across a yard of output. I use resultion = 6, regardless of scale, which is overkill, but aligns with other tools I use, even format(x, "f") in Python. This is more than a cosmetic choice, the svg path data formatting functions will identify shorthand opportunities based on this resolution, so 0.000001 and 0.000002 will be different at resolution None, but equivalent at resolution 6. A path that is open at a higher resolution may be conceivably closed at a lower resolution.
format_number("5000", 6)
# "5e3"
format_number("-17e+06", 6)
# "-1.7e7"
format_number(2/3, 6)
# ".666667"
format_number(-1/10e6, 6)
# "0"
format_as_exponential and format_as_fixed_point are available if you have a preference.
reformat svg path data strings
format_svgd_absolute and format_svgd_relative will convert between absolute and relative path data strings and optimize* existing path data strings.
format_svgd_shortest will, command by command, select the shorter of the absolute and readonly versions.
input = "M50,55C50,55 52 50 55 55Q0 2 2.5 2.5L2.5 0.5ZA1 1 1 1 1 54 44"
format_svgd_relative(input, resolution=2)
# m50 55s2-5 5 0q-55-53-52.5-52.5v-2za1 1 1 1 1 4-11
format_svgd_absolute(input, resolution=2)
# M50 55S52 50 55 55Q0 2 2.5 2.5V.5ZA1 1 1 1 1 54 44
format_svgd_shortest(input, resolution=2)
# M50 55s2-5 5 0Q0 2 2.5 2.5V.5Za1 1 1 1 1 4-11
* Optimize is subjective: zZ in svg is strictly shorthand for a line command back to the most recent move command, but it is a strong convention to explicitly close paths that are implicitly closed with a curve ... with a zZ command, which in the "closed by a curve" case is a zero-length line. All functions here add these explicit zZ commands. If the Z command is in the middle of a path, and additional mM command is needed afterward, adding another additional character. Also, each path in the "shortest" versions will always start with an M, even when m might save a character. This is so paths can be concatenated.
convert svg path data strings
get_cpts_from_svgd and get_svgd_from_cpts convert between svg path data strings and non-rational Bézier control points.
cpts = (
((0.5, 0.5), (1/3, 0.0), (2.0, 0.0), (2.5, 0.5)),
((2.5, 0.5), (2/3, 2.0), (2.5, 2.5)),
((2.5, 2.5), (0.5, 2.5)),
((0.5, 2.5), (0.5, 0.5)),
)
get_svgd_from_cpts(cpts, resolution=2)
# M.5 .5C.33 0 2 0 2.5 .5Q.67 2 2.5 2.5H.5Z
get_cpts_from_svgd('M.5 .5C.33 0 2 0 2.5 .5Q.67 2 2.5 2.5H.5Z')
# cpts = [
# [(0.5, 0.5), (0.33, 0.0), (2.0, 0.0), (2.5, 0.5)],
# [(2.5, 0.5), (0.67, 2.0), (2.5, 2.5)],
# [(2.5, 2.5), (0.5, 2.5)],
# [(0.5, 2.5), (0.5, 0.5)]
# ]
arc commands
format_svgd_* functions understand all svg commands, including the arc commands, A and a. Converting these to actual non-rational Bézier control points is not possible, but an [A, to, B] representation is useful for joining paths. The representation I've chosen is
[(ax, xy), (x_rad, y_rad, x_rot, large_flag, sweep_flag), (bx, by)]
get_svgd_from_cpts and get_cpts_from_svgd will translate from / to this representation, which simplifies matching path commands by their endpoints.
Metadata
Release files for svg-path-data 0.7.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| svg_path_data-0.7.2.tar.gz | 14.4 kB | Without provenance | Details for svg_path_data-0.7.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| svg_path_data-0.7.2-py3-none-any.whl | Python 3 | none | any | Details for svg_path_data-0.7.2-py3-none-any.whl |
Total release size: 30.7 kB
Release files / svg_path_data-0.7.2.tar.gz
| Download URL | svg_path_data-0.7.2.tar.gz |
|---|---|
| Size | 14.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e9935da9bac67b1e031727149bcce922534f03ea026a79cf23a25c71e5844e36
|
|
BLAKE2b-256 checksum How to use checksums |
251780be964b366aecff45e13ce8406507e6ae132ddb8f427aa736ae80f1e86f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / svg_path_data-0.7.2-py3-none-any.whl
| Download URL | svg_path_data-0.7.2-py3-none-any.whl |
|---|---|
| Size | 16.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
780bd5abeac4de7cd3ef53c42edf1eff8bb823a2426fd73bddacdedd4a0d9858
|
|
BLAKE2b-256 checksum How to use checksums |
f7647784f85efb8d554a9c417030272374268a19c3107b83b2b5a8eb16753c9f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Security for svg-path-data 0.7.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.