Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatizing 'simplify path' for a svg-file (using inkscape)

Tags:

svg

inkscape

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?

like image 928
Fabian Avatar asked Jan 19 '23 19:01

Fabian


2 Answers

UPDATE:

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
like image 96
Fabian Avatar answered Jan 27 '23 22:01

Fabian


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
like image 43
pix Avatar answered Jan 27 '23 21:01

pix