Mobile app version of vmapp.org
Login or Join
Jamie315

: How to fit svg drawings to their canvas on the command line? Cropping .svg files on the command line is simple: $ inkscape --verb=FitCanvasToDrawing --verb=FileSave --verb=FileClose *.svg I need

@Jamie315

Posted in: #BatchProcessing #Inkscape #Resize #Svg

Cropping .svg files on the command line is simple:
$ inkscape --verb=FitCanvasToDrawing --verb=FileSave --verb=FileClose *.svg

I need to do the opposite. I want to fit the drawing into a 64 x 64 points canvas (already set in all .svg files). Unfortunately Inkscape doesn't provide a FitDrawingToCanvas command.
Moreover, the fitting should keep the aspect ratio of the drawing.

If it matters: I'm using Ubuntu raring.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Jamie315

2 Comments

Sorted by latest first Latest Oldest Best

 

@Hamm6457569

I found a way to do this in this question: Inkscape - Center Drawing to Page via Command Line/Terminal

With "foo.svg" as the image to edit:

inkscape --verb=EditSelectAll --verb=AlignHorizontalCenter --verb=AlignVerticalCenter --verb=FileSave --verb=FileQuit foo.svg


To edit all svg images in the current directory:

inkscape --verb=EditSelectAll --verb=AlignHorizontalCenter --verb=AlignVerticalCenter --verb=FileSave --verb=FileClose *.svg


But this second command opens a ton of windows, which will make your computer crash if you're editing too many images. For Linux only, this command will work better:

for img in $(ls *.svg) ; do inkscape --verb=EditSelectAll --verb=AlignHorizontalCenter --verb=AlignVerticalCenter --verb=FileSave --verb=FileQuit $img ; done


For the above command, if any of the files are symlinks, Inkscape will edit the target file that the symlink points to. If you don't want Inkscape to do this, you can filter out any symlinks with this command:

for img in $(ls *.svg) ; do if [[ $(readlink $img) == "" ]] ; then inkscape --verb=EditSelectAll --verb=AlignHorizontalCenter --verb=AlignVerticalCenter --verb=FileSave --verb=FileQuit $img ; fi ; done



While I'm at it, I might as well post the bash script I made for this:

#!/bin/bash
# inkscape-center <file-or-directory>...

_analyse() {
if [ -d "" ] ; then
_centerAll "" ;
else
_center "" ;
fi
}

_centerAll() {
cd "" ;
for img in $(ls "*.svg") ; do
_filterSyms "${img}" ;
done
}

_filterSyms() {
if [[ $(readlink "") == "" ]] ; then
_center ""
fi
}

_center() {
inkscape --verb=EditSelectAll --verb=AlignHorizontalCenter --verb=AlignVerticalCenter --verb=FileSave --verb=FileQuit ""
}

for arg ; do
_analyse "${arg}" ;
done


I called it inkscape-center and run it like this:

inkscape-center <file-or-directory>


It takes as many arguments as you want, so you can do something like this:

inkscape-center 1st.svg 2nd.svg 3rd.svg 4th.svg


Be careful- If you specify a directory instead of a file, it'll edit every svg file in that directory.

10% popularity Vote Up Vote Down


 

@Ravi4787994

You could use viewBox to accomplish what you want. I don't know if there is a way to do this from within Inkscape, but since SVG is a standard format and there may be another tool that will do the job you want. A quick search for "svg command line tools" revealed some interesting results, including this one for creating CSS icons.

A second option would be to write your own tool in your language of choice to do this. The basic gist is to set the viewBox to the height of your document, then set the width and height of the document to want. Finally, set the preserveAspectRatio attribute.

Here's what the modifications described above look like on a document that was originally 744x1052.

<svg
width="64"
height="64"
viewBox="0 0 744 1052"
preserveAspectRatio="xMinYMin slice"

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme