I'm trying to downscale some SVGs that have a default of about 420 by 500 pixels, they are not all the same sizes, but I would like to maintain a width of 50px on the output files.
This is what I've managed so far (from this):
import os
import svgutils
path = 'path/to/files'
svg_files = [f for f in os.listdir(path) if f.endswith('.svg')]
for i in svg_files:
svg_file = os.path.join(path, i)
svg = svgutils.transform.fromfile(svg_file)
original = svgutils.compose.SVG(svg_file)
original.scale(.1)
svg_out = os.path.splitext(svg_file)[0] + '_scale.svg'
new_svg = svgutils.compose.Figure(float(svg.height) * .1,
float(svg.width) * .1, original)
new_svg.save(svg_out)
But it only adds the <g>
with transform, and doesn't resize the original. Moreover, the end results won't open in inkscape.
Any idea what I am doing wrong here?
Edit:
I've had a deeper look around, so far I've managed to resize the svgs using librsvg-2.40.1-2-w32-bin.zip
from this sourceforge project and adding the /bin
folder to windows path, this allows me to do the following:
rsvg-convert -a ".\infile.svg" -w "30" -f svg -o ".\outfile.svg"
however, no color information is retained. So I'm thus far.
Edit 2:
The color issue happens during the conversion using rsvg-convert
, it seems that it also converts hex to rgb tuple in the process. Which some viewers do not support (such as MapboxGL studio)
After some fiddling around, I finally managed to get this right. While the solution is rather dirty, it gets the job done.
Here is the solution: