Skip to content

A Python library which extracts strokes (outlines) from an SVG file as a series of line segments appropriate for driving pen plotters

License

Notifications You must be signed in to change notification settings

mossblaser/svgoutline

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

svgoutline

This Python library extracts all strokes (outlines) from an SVG vector graphics file as a series of straight line segments appropriate for driving pen plotters or desktop cutting machines.

Features

  • Supports most common SVG features including beziers, shapes, simple text, dashed lines and object/layer visibility. Converts all of these into simple straight line segments with no transformations required:

    Input SVG Arrow Output SVG

  • Ignores out non-stroked objects.

    Input SVG Arrow Output SVG

  • Curves are approximated by straight lines with user-defined fidelity.

    Input SVG Arrow Output SVG Output SVG Output SVG

  • Captures stroke colours in RGBA format and millimetres respectively (e.g. to allow colour/thickness dependent cutting/plotting settings).

    Input SVG Arrow Output SVG

Usage

Install from PyPI:

$ pip install svgoutline

Provide a valid SVG deserialised from XML using Python's ElementTree API:

>>> import xml.etree.ElementTree as ET
>>> from svgoutline import svg_to_outlines

>>> tree = ET.parse("example.svg")
>>> root = tree.getroot()

>>> outlines = svg_to_outlines(root)

Where outlines will be a list of lines of the form:

[
    ((r, g, b, a), line_width, [(x, y), ...]),
    ...
]

With r, g, b and a being between 0.0 and 1.0, and with line_width and the coordinates being given in millimetres.

See help(svg_to_outlines) (or svg_to_outlines.py) for full usage information.

Alternatively, a quick'n'dirty demo script is provided in samples/demo.py which generates the examples above given an SVG file as input. See python samples/demo.py --help for more information.

Limitations

  • Only SVG Tiny 1.2 is supported due to the use of Qt SVG internally. The following significant features are missing which you might otherwise expect:
    • Clipping masks are not supported and will be ignored.
    • Many text features beyond simple single-line text strings are not supported, for example text on path, line wrapping or style changes mid text element.
  • Depends on Qt for Python (a.k.a. PySide6). This is a relatively non-trivial dependency but is easy to install from PyPI on most platforms. Unfortunately it makes svgoutline subject to the same bugs (e.g. QTBUG-72997 which at the time of writing causes text outlines and dash patterns to render too small).
  • Oblivious to fills and overlaps. Consequently, if two shapes overlap, their full outlines will be included in the output regardless of what parts of their outlines are actually visible. For plotting purposes this should not be a significant problem as input SVGs are unlikely to contain filled elements.
  • Output does not distinguish between closed paths and paths whose start and end coordinates are the same. This distinction is not important for most plotting applications.

Tests

The tests are written using py.test and test dependencies can be installed and the tests executed with:

$ pip install -r requirements-test.txt
$ py.test tests

The code adheres to the Python PEP8 style guide and is checked using flake8 (installed with py.test in the commands above). Run it using:

$ flake8 tests svgoutline

License

GNU Lesser General Public License v3 (LGPLv3)

About

A Python library which extracts strokes (outlines) from an SVG file as a series of line segments appropriate for driving pen plotters

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages