Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

An SVG has these properties, that determine its size:

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

  1. Shift+Ctrl+M (Transform)
  2. Scale (check "scale proportionally")
  3. Enter width
  4. Shift+Ctrl+D (Document properties)
  5. 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)"

to the whole image.

share|improve this question
What OS do you use? – terdon Sep 16 '12 at 17:22
Ubuntu 10.04.4 LTS (I am going to update it ... ) – Martin Thoma Sep 16 '12 at 17:28
SVG files are just text files. You may be able to edit the file and change the width, height, and viewBox values to something like width="100%" height="100%" viewBox="0 0 400 212.58569". – martineau Sep 16 '12 at 18:07
I know that they are only text files. But if I edit only these values, the size of the image doesn't change. Only the canvas size changes. Editing by hand isn't an option, I explicitly stated that I want to change it automatically. – Martin Thoma Sep 16 '12 at 19:01
Yes, I know you asked for an automated way to do it, but thought that if you could figure out what to change, text-wise, then you could automate it using any one of a number of the text-editing tools available (such as sed in @terdon's answer). One problem you may run into is that the values need may to be different with any other .svg files you want to do the same thing to. – martineau Sep 16 '12 at 21:15
up vote 4 down vote accepted

rsvg-convert from the librsvg2-bin package can resize svg's.

See this answer for full instructions.

share|improve this answer
rsvg-convert -a -w 500 -f svg Vector-triangle-inequality.svg -o Result.svg worked. But rsvg-convert seems to do more than only resizing. – Martin Thoma Jan 20 '13 at 15:48
@moose, however, as noted at that answer, rsvg-convert uses points, not pixels. If you're lucky, you can just divide your intended size by 1.25. However, if you want something like 24px (19.2 in points), you're out of luck, since it is unwilling to put a non-integer as the width or height (not sure if SVG allows this). – Matthew Flaschen Aug 10 '14 at 3:53
@MartinThoma Yes, indeed. The SVG output file is about double the original size. I wonder why? Reading rsvg-convert --help does not provide any clues. – Serge Stroobandt Apr 5 at 11:11

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.

share|improve this answer
Your inkscape-command results in a PNG, but I want a SVG. Your svg command does only change the canvas size, but not the size of the image. You can't simply use a text editor and only change the values I've mentioned above. I think I have to clarify the question. – Martin Thoma Sep 17 '12 at 3:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.