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.


Share this post on…

  • Mastodon
  • Facebook
  • LinkedIn
  • BlueSky
  • Threads
  • Reddit
  • HackerNews
  • Lobsters
  • WhatsApp
  • Telegram

9 thoughts on “Inkscape - cropping SVG files on the Command Line”

  1. says:

    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

    Reply
  2. Sean says:

    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!

    Reply
  3. says:

    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.

    Reply
  4. Ralph says:

    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

    Reply
  5. Victor says:

    Thanks for the tips, really helpful !

    Ended up using a bash loop to prevent opening too much files at once : for i in *.svg; do inkscape --verb=FitCanvasToDrawing --verb=FileSave --verb=FileQuit "$i"; done

    Reply

What are your reckons?

All comments are moderated and may not be published immediately. Your email address will not be published.

Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <p> <pre> <br> <img src="" alt="" title="" srcset="">