SVG Path Objects and Parser

7.0 · active · verified Sat Apr 11

svg.path is a collection of objects that implement the different path commands in SVG, and a parser for SVG path definitions. It provides classes for various path segments (Line, Arc, CubicBezier, QuadraticBezier) and a `Path` object to collect them. As of version 7.0, it's actively maintained and frequently updated to reflect improvements and address edge cases in SVG path parsing.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse an SVG path string into a Path object and iterate through its segments. It also shows how to construct a path programmatically using segment objects like Line and Arc. Note that `svg.path` uses complex numbers for coordinates.

from svg.path import parse_path, Path, Line, Arc
import cmath # For complex numbers used by svg.path

# Parse an SVG path string
path_data = "M 10 10 L 100 10 L 50 50 Z"
path = parse_path(path_data)

print(f"Path has {len(path)} segments.")
for i, segment in enumerate(path):
    print(f"Segment {i}: {type(segment).__name__} from {segment.start} to {segment.end}")
    # Example: Get a point along the segment
    if hasattr(segment, 'point'):
        mid_point = segment.point(0.5)
        print(f"  Midpoint: ({mid_point.real:.2f}, {mid_point.imag:.2f})")

# Construct a path programmatically
p = Path([
    Line(start=cmath.rect(1, cmath.pi/2), end=cmath.rect(10, cmath.pi/2)), # Example using complex numbers
    Line(start=(10+0j), end=(10+50j)),
    Arc(start=(10+50j), radius=(20,20), rotation=0, large_arc=True, sweep=True, end=(30+70j))
])
print(f"\nProgrammatic path has {len(p)} segments.")

view raw JSON →