Is there a way to automatically export all objects on a layer to a png files - one layer at a time

Asked by Charlie

I want to step through layers and export .png files of all the objects on the layer.
Is there any way to do that from a command line or is there any other way to do it?

Question information

Language:
English Edit question
Status:
Answered
For:
Inkscape Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
mahfiaz (mahfiaz) said :
#1

Something like
inkscape --file=file.svg --export-pdf=export.pdf --export-id-only --export-id="layer1"
should do it. If all layer id's are as default, then you could even make script for layer1 to layer100 (exporting for not existing objects simply fails, no need to detect the actual number of these).

Revision history for this message
Charlie (charlie-hahs) said :
#2

That sounds pretty good - does that include pngs files (not pdf files), and also, can I give a list of layers?
Not sure if I should say problem solved or still need answer

Revision history for this message
mahfiaz (mahfiaz) said :
#3

Just see inkscape --help, forgot to mention.
And for list of layers you could make a simple bash script, as mentioned earlier: "If all layer id's are as default, then you could even make script for layer1 to layer100 (exporting for not existing objects simply fails, no need to detect the actual number of these)."
And of course the script may not export only layer1 to layer100, but any layer you make it to. Google for bash tutorials (or for BAT, when on windows).

Revision history for this message
Charlie (charlie-hahs) said :
#4

I hope you do not mind my saying so, but the help for the command line is not too clear - not to me anyway, even after trying pretty hard. I too am not always as clear as I think - what I am wondering is if I can export more than one layer at a time - I get the idea using the --export-id="layer1" means it will export only that layer into a pdf file as you have written. I have not tried the command line approach yet - I suppose it looks like you have in your original response to my question. I will try it and let you know....

Revision history for this message
pbhj (pbhj) said :
#5

#!/bin/bash
# by pbhj, http://alicious.com; CC-BY
# $1 is the file to extract layers from
# $2 is not needed but can be used eg with "-d 300" to send the png resolution or any other inkscape switch through

# first set a list of the layers, this is extracted from the list of all id's using inkscapes
# -s switch and then grep-ing for only the matched part (-o) of an extended regexp (-E)
# that matches "layer" followed by between 1 and 3 numbers.

for layer in $(inkscape -S $1 | grep -o -E "^(layer[0-9]{1,3})")
do
        # this line exports the id's only (without that you get overlapping bits from other layers
        # and uses the id's of the layers in the $layer list from above
        # you can add extra requirements when you run the script, they get passed through to $2,
        # $1 is the filename
        inkscape --without-gui --export-id=$layer --export-id-only --export-png=$1__$layer.png $2 $1

        # format the output slightly, escaping as "echo *" is a bash command
        echo \*\*\* exported $1, $layer \*\*\*
        echo " "
done

Revision history for this message
pbhj (pbhj) said :
#6

The script above, export-layers.sh, can be used like:

./export-layers.sh layers-test.svg "-d 120"

Which will export all layers of the file layers-test.svg and will also pass the "-d 120" option to inkscape ensuring the output is 120dpi.

HTH

pbhj

Revision history for this message
k (kasi-u) said :
#7

Made this script, it's more general that the other one, but it also does something more specific. It deletes one layer after the other and then exports the image. I use it to create animated images for beamer presentations. You'll need xmlstarlet for manipulating the svg file.

#!/bin/bash
# CC-BY
# $1 is the file to extract layers from
# $2 is not needed but can be used eg with "-d 300" to send the png resolution or any other inkscape switch through

TMPFILE=$(mktemp /tmp/output.XXXXXXXXXX).svg
cp $1 $TMPFILE

i=0

for layer in $(xml sel -t -m "//*[@inkscape:groupmode=\"layer\"]" -v "concat(@inkscape:label,'
')" $TMPFILE|sort -Vr)
do

    echo -n "Exporting visible up to '$layer' as "

    i=$((i+1))
    inkscape --without-gui --export-pdf=$1__$i.pdf $2 $TMPFILE

    id=$(xml sel -t -m "//*[@inkscape:label=\"$layer\"]" -v "@id" $TMPFILE)
    echo "$1__$i.pdf"

    xml ed -S -L -d "//*[@id=\"$id\"]" $TMPFILE
done

[ -f $TMPFILE ] && rm $TMPFILE

Revision history for this message
Bertrand G (berteh) said :
#8

The following script allows for batch export (from command line) a set of objects using inkscape, each to its own file. https://github.com/berteh/svg-objects-export

Any option from synfig's command line can be passed to the script. Objects to export can be identified by some regular expression on the id, or using xpath.

Can you help with this problem?

Provide an answer of your own, or ask Charlie for more information if necessary.

To post a message you must log in.