svgutils

repository·master·Indexed 43 days ago

https://github.com/btel/svg_utils

A Python-based utility package for editing, concatenating, and composing SVG files. It provides a high-level declarative API via the `compose` module to arrange SVG and image elements into multi-panel figures using `Figure`, `Panel`, and `Text` objects. It includes tools for geometric transformations like scaling, moving, and rotating, as well as a `Unit` class for measurement conversions. Specifically designed for scientists to automate the creation of publication-quality figures.

Tokens
5.6K
Snippets
27
Records
30
Agent score
56%

What's inside svgutils

  1. Perform basic SVG transformations with svgutils.transform

    master
    The svgutils.transform module provides a low-level API for opening and manipulating SVG files through geometric transformations. It is designed for fine-grained control over SVG elements. For a high-level workflow involving the creation of publication-quality figures, refer to the tutorials/publication_quality_figures tutorial.
  2. How the `compose` module works

    master
    The compose module provides a declarative, hierarchical API for creating complex multi-panel SVG figures. Unlike procedural low-level APIs, compose allows you to define figures as a tree structure: a Figure contains multiple Panel objects, which in turn contain graphical elements like SVG drawings, Text annotations, or other sub-panels. This approach simplifies tasks like inserting new panels and adjusting their relative positions.
  3. Install svgutils via Conda

    master

    You can install svgutils from the conda-forge channel. You can either add the channel to your global configuration first, or specify it directly during the installation command.

    # Option 1: Add channel to config then install
    conda config --add channels conda-forge
    conda install svgutils
    
    # Option 2: Install specifying the channel directly
    conda install svgutils -c conda-forge
  4. Install svgutils from source

    master

    If you are installing from the source repository, you can perform a system-wide installation (requires administrator privileges) or a local user installation.

    Note: If installing locally with the --user flag, ensure you add your local site-packages directory (e.g., $HOME/python/lib/python3.x/site-packages/) to your PYTHONPATH.

    # System-wide installation (requires sudo/admin)
    python3 setup.py install
    
    # Local user installation
    python3 setup.py install --user
  5. Automate the creation of publication-quality composite figures

    master

    Instead of manually arranging plots in vector graphics editors like Inkscape or Adobe Illustrator, you can use svgutils to automate the composition of multiple plots into a single figure. This workflow is particularly useful when you need to frequently update your data or analysis, as you can re-run your scripts to regenerate the entire composite figure without manual repositioning or annotation.

    1. Generate Plots: Use matplotlib to create individual plots.
    2. Export to SVG: Save your matplotlib figures as .svg files.
    3. Compose with svgutils: Use a Python script with svgutils to import the SVG files, arrange them into a layout, and add annotations (e.g., labels like 'A', 'B').
    4. Convert to Final Format: Use a tool like inkscape to convert the final composite SVG into PDF or PNG for publication.
  6. Convert SVG to PDF or PNG using Inkscape CLI

    master

    Once you have generated your final composite figure using svgutils, you can use the Inkscape command line interface to convert the .svg file into high-quality PDF or PNG formats without opening the Inkscape GUI.

    To export to PDF:

    inkscape --export-pdf=fig_final.pdf fig_final.svg

    To export to PNG:

    inkscape --export-png=fig_final.png fig_final.svg
    inkscape --export-pdf=fig_final.pdf fig_final.svg
    inkscape --export-png=fig_final.png fig_final.svg
  7. Compose SVG figures with the compose module

    master

    The svgutils.compose module provides a high-level wrapper around svgutils.transform to simplify the process of layering and arranging different SVG elements into a single figure. You can use the Figure class to combine various elements like SVG files and Image files, applying transformations such as .scale() and .move() to each element before saving the final composition.

    from svgutils.compose import Figure, SVG, Image
    
    Figure(
        "10cm", "5cm",
        SVG('svg_logo.svg').scale(0.2),
        Image(120, 120, 'lion.jpeg').move(120, 0)
    ).save('test.svg')
  8. Install svgutils via pip

    master

    You can install svgutils from the Python Package Index (PyPI) using pip3.

    Prerequisites: pip3 will attempt to install the lxml library. For a successful installation, you must have the development libraries libxml2 and libxslt1 installed on your system.

    On Ubuntu and Debian-derived Linux distributions, install these dependencies using:

    sudo apt-get install libxml2-dev libxslt-dev
    pip3 install svgutils --user
  9. Group elements into sub-panels with `Panel()`

    master

    A Panel() object allows you to group multiple elements (like a graph and its label) so they can be treated as a single unit.

    Key characteristics:

    • Transformations: Applying .move() or .scale() to a Panel affects all elements inside it simultaneously.
    • Tiling: The Figure.tile() method treats each Panel as a single unit in the grid, rather than tiling the individual elements within the panel.
    • Limitations: Unlike Figure(), a Panel() cannot have a defined size and does not have a .save() method.
    Figure("16cm", "6.5cm", 
           Panel(
              Text("A", 25, 20),
              SVG("sigmoid_fit.svg")
           ),
           Panel(
              Text("B", 25, 20),
              SVG("anscombe.svg").scale(0.5)
           ).move(280, 0)
           )
  10. Use `Grid()` for layout debugging

    master

    The Grid(spacing_x, spacing_y) object generates a grid of horizontal and vertical lines with numerical labels in pixel units. Adding Grid() as an element in your Figure helps you visually estimate the required move() and scale() values for your elements.

    Figure("16cm", "6.5cm", 
           SVG("sigmoid_fit.svg"),
           SVG("anscombe.svg").scale(0.5).move(280, 0),
           Grid(20, 20)
           )
  11. Define and save a Figure with `Figure()`

    master

    The Figure() object is the top-most container in the compose module. To create a figure, specify its width and height (supporting units like "px" and "cm"; defaults to "px" if no unit is provided) and its contents. Use the .save("filename.svg") method to export the result.

    from svgutils.compose import *
    
    Figure("16cm", "6.5cm", 
           SVG("sigmoid_fit.svg")
           ).save("fig1.svg")