4

Is there a way to center the whole drawing to the page in Inkscape with just a Terminal command? I need to do this for a couple hundred images. Manually, you do this by hitting "CTRL+A", "CTRL+ALT+H", "CTRL+ALT+T".

Halfway through writing this question, I figured it out. I'll post the answer.

2 Answers 2

7

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 *.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 *.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 "${1}" ] ; then
        _centerAll "${1}" ;
    else
        _center "${1}" ;
    fi
}

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

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

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

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.

1
  • this does not work anymore, since the verbs AlignHorizontalCenter are not relative to page, but to selection. I am trying to find out if older inkscape version fixes this (but it is hard to install old inkscape version in the first place, due to all PPAs being updated) Commented Mar 14, 2020 at 8:43
1

Verbs were removed in 1.1, so now the command for foo.svg is

inkscape "--actions=select-all:all;object-align:hcenter vcenter page;export-filename:foo.svg;export-do" foo.svg

and the for loop is

for f in *.svg; do inkscape "--actions=select-all:all;object-align:hcenter vcenter page;export-filename:$f;export-do" $f; done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.