How to crop a SVG image to the bounding box of the vector grafics elements

929 Views Asked by At

I have a SVG image hereenter image description here which is generated with 'empty space', i.e. only in the top-left corner is image content, whereas the rest is blank. I think it should be trivially possible to have an automated way to crop the image size to the bounding box of the objects - at least for some svg tooling like rsvg. However I am unable to find the 'command line trick' for this, etc.

I would like to do this on the command line (i.e. as part of a build script)

In principle I would be interested in a solution to the same problem but for pixel-based formats such as PNG as well.

1

There are 1 best solutions below

0
On

rsvg does not have command line utilities for this problem, but Inkscape in its non-GUI mode has:

inkscape -o cropped.svg -D source.svg

will crop the file to the bounding box of all objects of the document. See the man page for a full documentation of the inkscape command line options. Especially note the --shell mode for batch processing multiple images.

For pixel-based formats there is the imagemagick -trim option:

convert source.png -trim +repage cropped.png