4

I would like to automatize the inkscape command "simplify path". Concretely, I would like a command line tool which takes a svg-file as input, applies "simplify path" to all paths in the figure and saves a new (smaller) svg-file. Is this possible using inkscape? Is there a free command line tool (I'm using linux) which does the job?

Fabian
  • 145
  • 1
  • 5

3 Answers3

6

UPDATE (2023-06-07):

Again the command line changed in v1.2. Now it should be something like (untested!):

inkscape backpacks-svgrepo-com.svg --batch-process --actions='select-all;path-simplify;export-plain-svg'

You can get a list of all list of all actions by running:

inkscape --action-list

and chose those fitting to your needs. Saving is a bit more tricky as it's not listed in the actions. The manual says something like:

In addition, there are actions matching all export options (to use them, simply remove the prefix '--' in front of the option and replace '=' with ':')


UPDATE (2021):

Since the question/answer is quite old the inkscape command line changed.

inkscape file.svg --batch-process --actions='EditSelectAll;SelectionSimplify;FileSave;FileClose'

Also see comment of Oren Ben-Kiki or Pix answer.

ORIG:

Should be possible:

http://tavmjong.free.fr/INKSCAPE/MANUAL/html/CommandLine.html

shows how to call functions of inkscape (called "verbs") from the command line. To get a list of all verbs call inkscape --verb-list on commandline. What you are looking for is SelectionSimplify.

Therefore you have to write a small script that is filtering every id out of the svg and is calling inkscape with the ids. Something like this (optimize all pathes and quit from inkscape at the end)

inkscape filename.svg --verb=EditSelectAll --verb=SelectionSimplify --verb=FileSave --verb=FileClose --verb=FileQuit
Fabian
  • 546
  • 5
  • 14
  • Ask my answer to [How to save SVG file with Inkscape CLI?](http://stackoverflow.com/a/18630916/802365) the so-called CLI mode's verbs need the GUI, don't use `-z` or `--without-gui`. – Édouard Lopez Apr 26 '15 at 13:55
  • This seems to be broken as of some Inkscape version - it fails with a `GUI required` error. Also there seems to no longer be a `FileQuit` verb? – Oren Ben-Kiki May 19 '21 at 06:40
  • 1
    The right way as of 1.0.2 seems to be `inkscape file.svg --batch-process --actions='EditSelectAllSelectionSimplify;FileSave;FileClose'`. If on Windows, this is `inkscape.com` and not `inkscape.exe`. – Oren Ben-Kiki May 19 '21 at 06:57
  • `Invalid option --batch-process` – user924 Nov 03 '22 at 16:27
  • doesn't work for 1.2.2 (not old, not new command) – user924 Mar 14 '23 at 18:34
4

Extending from Fabian's answer, to control the threshold of the simplification function, I found I needed to make a fake home directory with a minimal preferences file containing my desired threshold. Here is a simple script I just put together.

simplify.sh:

#!/bin/bash
FILENAME=$1
THRESHOLD=$2
FAKEHOME=$(mktemp -d)
mkdir -p $FAKEHOME/.config/inkscape
cat > $FAKEHOME/.config/inkscape/preferences.xml <<EOF
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<inkscape
  xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
  xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
  version="1">
  <group
    id="options">
    <group
      id="simplifythreshold"
      value="${THRESHOLD}" />
  </group>
</inkscape>
EOF
# for Inkscape < 1.0
#HOME=$FAKEHOME inkscape $FILENAME --verb=EditSelectAll --verb=SelectionSimplify --verb=FileSave --verb=FileClose
# for Inkscape > 1.0
HOME=$FAKEHOME inkscape --with-gui --batch-process $FILENAME --verb='EditSelectAll;SelectionSimplify;FileSave'
#rm -rf $FAKEHOME
a sad dude
  • 2,775
  • 17
  • 20
pix
  • 5,052
  • 2
  • 23
  • 25
  • Thank you for the script. With the current version of Inkscape I had to add --with-gui and --batch-process flags to make it work, as well as group the verbs into one --verb argument. I edited your answer -- hope you don't mind. It seems the GUI and thus a graphical environment is a requirement now. – a sad dude Dec 13 '20 at 00:11
-3

Alternative to Inkscape

I've got much better results using SVGO (reduced a file from 2.7 MB to 350 KB).

You may use this online service for individual files: https://jakearchibald.github.io/svgomg/

Adrian
  • 2,233
  • 1
  • 22
  • 33
  • 2
    Is there an option for SVGO that actually simplifies path information in the way inkscape "simplify" does? Unless I'm missing something, it seems to only do lossless optimisation of the encoding. – pix Oct 13 '18 at 02:44
  • @pix You should ask this directly over at SVGO: https://github.com/svg/svgo/issues – Adrian Oct 17 '18 at 07:49
  • 1
    @Adrian I guess it's because your answer includes only the link to a library/program without further instructions how to use it or to solve this particular problem. – Fabian Dec 04 '19 at 11:07