Inkscape - cropping SVG files on the Command Line


A little post as an aide-mémoire.

I've found a lovely set of SVG playing cards. The only problem is each image is displayed within a page. This means the relatively small cards have an enormous white margin.

Within Inkscape (the vector image editor for Linux) it's possible to crop the margins by going to:

File > Document Properties > Resize Page To Content > Resize page to Drawing or Selection 

That's a pain in the arse for adjusting 52 images. Luckily, Inkscape will take command line arguments (called verbs). This means we can point it to all SVG files in a directory, instruct it to Fit to Drawing, and save the file again.

It all takes place on one line.

inkscape --verb=FitCanvasToDrawing --verb=FileSave --verb=FileClose *.svg

Run that, the Inkscape window will pop up for each file, and automatically close once saved. I'm not sure if there's a way to do it on a headless install.

8 thoughts on “Inkscape - cropping SVG files on the Command Line

  1. There is also an option to Inkscape called --query-all that will give you the dimensions without opening up a GUI. Using these, you can crop the SVG with some simple code. I just wrote a tool to do this in Python:

    https://github.com/skagedal/svgclip

    When trying to google a method for doing this tonight, this was one of the first posts I found, so I thought I'd get back and tell you what I ended up with :)

    Regards, Simon

  2. This is incredible. So I'm looking for some nice cards, and I too find the nice SVG cards you linked to. Then I run into the problem of the large white canvas/background. Not knowing anything about SVGs, I download Inkscape and discover the "resize page to content" function in the properties menu. So I do a couple manually before realizing it will take way too long, so I google it, which leads me here, to your page - where I find you have already sorted out this exact problem.

    So thanks!

  3. You saved my wrist! Thanks a lot.

    It's like watching a time machine movie; Inkscape is coming and going.
    I wish Inkscape could to it without opening the GUI with -z option. The -z option disables --verb.

  4. Hi, somehow I only get the first svg-File processed. After that, the gui lingers and I have to quit it. Has anything changed? I use version 0.91

Leave a Reply

Your email address will not be published. Required fields are marked *