/SVG-Image-Utils

A lightweight Python SVG parser that focuses on correctly layering SVG images

Primary LanguagePythonMIT LicenseMIT

SVG Image Utils

SVG Image Utils is a lightweight Python SVG parser that focuses on correctly layering SVG images

When appending SVGs, the name selectors in the style tag and class paths are the same in both the base template and the appended SVG images. As a result, an abnormal looking SVG image will be created.

SVG Image Utils modifies attributes that contradict each other between all the appended SVGs and thus outputs a new, intuitive looking layered SVG image.

Usage

Install svgimgutils:

$ pip install --save svgimgutils

Example

We will stack SVG images over the following SVG image:

Base template:

Base Template

This monkey is in a need for customization. The SVG images we will append are as following:

Hat:

Hat

Mouth:

Mouth

Glasses:

Glasses

Pants:

Pants

Shoes:

Shoes

Misc:

Misc

Tail:

Tail

The following snippet will create an SVG Image Utils for each SVG image and will append them onto the base template SVG.

    from svgimgutils import SVGImgUtils


    # Create SVG Image Utils for each SVG image
    base_template = SVGImgUtils.fromfile('Images/monkey.svg')
    pants = SVGImgUtils.fromfile('Images/pants.svg')
    shoes = SVGImgUtils.fromfile('Images/shoes.svg')
    tail = SVGImgUtils.fromfile('Images/tail.svg')
    mouth = SVGImgUtils.fromfile('Images/mouth.svg')
    glasses = SVGImgUtils.fromfile('Images/glasses.svg')
    hat = SVGImgUtils.fromfile('Images/hat.svg')
    misc = SVGImgUtils.fromfile('Images/misc.svg')

    # Append SVG images onto base template
    base_template.append(pants)
    base_template.append(shoes)
    base_template.append(tail)
    base_template.append(mouth)
    base_template.append(glasses)
    base_template.append(hat)
    base_template.append(misc)

    # Save new merged SVG image
    base_template.save('Images/merged.svg')

This will result in a new "merged" svg image:

Result:

Merged

License

This project is licensed under the MIT License - see the LICENSE.md file for details