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.

  • 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

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

See this answer for full instructions.

  • 5
    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
  • 2
    @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 '16 at 11:11
  • 1
    rsvg convert also seems to convert stroked paths to outlined paths. You may not want this (I know I don't) – scruss Jan 15 '17 at 2:02
  • 1
    This tool just seems to jam transforms into each element and doesn't actually change the original sizes. – rtaft May 7 '18 at 18:04

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.

  • 8
    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
  • you also need to add a tag: ..."><g transform="scale(2)"><path ..../></g></svg> to scale the image with the same factor. – Stofke Mar 17 '17 at 10:06
  • Not sure if this changed from 2012 to 2020, but that inkspace command did resize the image along with the canvas. – MestreLion Sep 19 '20 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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