How can I scale an SVG via command line?
An SVG has these properties, that determine its size:
width="146.91299pt"
height="78.079002pt"
viewBox="0 0 146.913 78.079"
I would like to scale a given SVG to a with of 400pt
by command line without breaking the aspect ratio. This means, the three values above have to be changed correctly and all points in the following svg code. I don't want to change only the canvas size, but the whole svg image size. And I don't want to do it with a GUI/by hand, but I want to do it with CLI.
How can I automatically scale an SVG by command line to a given width?
Reason: The preview of Wikipedia looks bad if the image is too small. See this image as an example.
How I would do this task with Inkscape-GUI
- Shift+Ctrl+M (Transform)
- Scale (check "scale proportionally")
- Enter width
- Shift+Ctrl+D (Document properties)
- Fit page to selection
Now I want a automatic CLI-way to do this.
By the way, inkscape simply applied
<g transform="matrix(1.6838397,0,0,1.6838397,-3.3543029,-3.3542794)"
id="surface0">
to the whole image.
rsvg-convert
from the librsvg2-bin
package can resize svg's.
See this answer for full instructions.
If you just want to change one image, open it in a text editor (emacs for example) and change the width entry.
For a geekier, CLI only approach, use sed:
$ sed 's/width=\"146.91299pt\"/width=\"400pt\"/' orig.svg > new.svg
This will not scale the image though, only set its width.
A better way, perhaps, but certainly one that can be run in batch mode for many images is using inkscape from the command line:
$ inkscape -z -e out.png -w 400 -h 400 in.svg
For many files (assuming you want them all to have the same width), do the following. It requires conversion to png and assumes there are no spaces in your filenames:
$ for n in $(ls *svg | sed 's/.svg//'); do inkscape -z -e $n.png -w 400 -h 400 $n.svg; done
ImageMagick's convert was my first choice but it seems to break when scaling svgs.