1

How do you crop all empty space from an SVG file, either from the command line or Python?

I have several SVG files formatted to the standard A2 letter document size, yet are mostly empty, and I need to bulk crop them down so their view box is the same as the minimum bounding box for their contents.

I can do this in Inkscape using the "Resize Page to Selection" option, but I don't see any way to access this function from the command-line. I thought it might be a call like:

inkscape -z --verb=FitCanvasToDrawing --verb=FileSave --verb=FileClose file.svg

as suggested here but that has no effect.

Cerin
  • 60,957
  • 96
  • 316
  • 522
  • https://github.com/icons8/svg-bounding-box should be able to give you the bounding box at least, but you might have to modify that in the SVG header yourself as it doesn't output a modified SVG file. Did you try the linked python library? https://github.com/skagedal/svgclip -- FileClose has also been replaced with FileQuit in the inkscape command list – MatsLindh May 16 '20 at 22:20
  • Note that the --verb command requires a GUI, and thus cannot be used with the --z option. – MatsLindh May 16 '20 at 22:23
  • @MatsLindh It looks like Python code depends on the `python-rsvg` package, which is no longer available anymore. – Cerin May 16 '20 at 23:03
  • @MatsLindh Nevermind, it was a bit complicated, but [this answer](https://stackoverflow.com/a/37648798/247542) explains how to install the Python bindings for GObject, a new catch-all Python wrapper that includes rsvg. I had to make a few modifications to svgclip to get it to support Python3, but otherwise that works. Thanks. – Cerin May 16 '20 at 23:33
  • Make a PR with those changes to the project for future reference for anyone encountering the same. Great job! – MatsLindh May 17 '20 at 10:45
  • 1
    @MatsLindh Heh, looks like [someone already did](https://github.com/skagedal/svgclip/pull/2/files) but it looks like the author and repo owner have abandoned it. – Cerin May 17 '20 at 14:13
  • That command is supposed to work with Inkscape 0.92. For Inkscape 1.0, the command line syntax has changed. Please refer to the man page or to the release notes for more information (https://wiki.inkscape.org/wiki/index.php/Release_notes/1.0#Command_Line , and 'man inkscape' on the command line) – Moini May 19 '20 at 12:39

0 Answers0