Stacking a lot of SVG images as layers issue

Question:

So I would like to stack a lot of svg images on top of each other in Python.
I am using this to do so:

import svgutils.transform as st

template = st.fromfile('firstLayer.svg')
second_svg = st.fromfile('secondLayer.svg')
template.append(second_svg)
template.save('merged.svg')

It technically works.

Only problem is that for example in my first image (template) I have 9 classes (cls 1 – 9) and in the second I have 4 (cls 1 – 4).
The name of the classes doesn’t change when stacking them so the images comes out weird because the style is mixing.
Does a solution that changes the classes name with respect to the existing SVG class name exists? for example If I stack the second layer on the first one the class names will change from 1 – 4 to 10 – 13 and so on for any other SVG image that will be added?

Asked By: Randomizer

||

Answers:

If anyone found himself due to the same issue, I didn’t find an already made Python solution that rewrites the class name in the elements and the class attribute of each path so I have created one myself:

https://github.com/Amirh24/SVGAppender

Feel free to use it 🙂

Answered By: Randomizer

Hey I was running into the exact same issue.

I found an example at https://svgutils.readthedocs.io/en/latest/tutorials/publication_quality_figures.html

Try this change:

import svgutils.transform as st
fig = st.SVGFigure("100px", "100px")

template = st.fromfile('firstLayer.svg')
second_svg = st.fromfile('secondLayer.svg')

fig.append([template.getroot(), second_svg.getroot()])
fig.save('merged.svg')
Answered By: Miguel Aragon
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.