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?
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 🙂
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')
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?
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 🙂
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')