12

Batch -export means that I may need to add certain transformations such as anti-alias, contrast-fixes and resizing. SVG here means I am using Inkscape. One layer here is a sprite. I currently export each layer individually to bitmap, File > Export Bitmap -- but it is time-consuming.

enter image description here

  1. Is there some way to programmatically batch-export layers from SVG-file?

  2. Or does there exist some batch-export-button in Inkscape?

  3. I specially need the same custom area for each sprite, the same as clicking many times the menus but it is stupid -- perhaps some macro for this?

9 Answers 9

17

You can use the commandline to export all layers. First, so see how many or which layers we have, with --query-all or -S:

$ inkscape --query-all presentation.svg | grep layer | awk -F, '{print $1}'
layer1
layer2
layer3

With the id (exakt name) of the layer, you can now export it with --export-id=ID or -i and -j, --export-id-only

$ inkscape presentation.svg -i layer1 -j -C --export-png=layer1.png

-C, --export-area-page defines the exported area (full page, as defined in document properties). You can also use --export-ps, --export-pdf, --export-eps and some other.


To automate it, you can generate a command list:

$ for id in `seq 1 15`; do echo "presentation.svg -jC -i layer$id -e layer$id.png"; done
presentation.svg -jC -i layer1 -e layer1.png
presentation.svg -jC -i layer2 -e layer2.png
presentation.svg -jC -i layer3 -e layer3.png

Or to export all layers:

$ for layer in $(inkscape --query-all presentation.svg | grep layer | awk -F, '{print $1}'); do echo "presentation.svg -jC -i $layer -e $layer.png"; done

When you save this in a file (here commands.txt), you can invoke inkscape like this:

$ inkscape --shell < commands.txt

do execute all listed commands.

2

Thanks to Freenode's #inkscape channel, the user su_.* and verbalsh.*. su_.* provided most of the below ideas. Please, notice that exporting to raster means an information loss. Besides, Inkscape has odd features such as anti-alias-always-on-in-exportation -feature and odd no-control-with-contrast-in-exportation -feature, meaning an extra information -loss. su_.* mentioned in this context: "always antialiased, same renderer as on-canvas".

According to su_*, "an earlier answer by the author of JessyInk to a related question" is here where you export each layer together with a common background -layer (which defines bg color or maybe export size).

Related feature requests

  • "Optionally disable anti-aliasing for bitmap export" -thread here or the newer "Kill anti-aliasing for bitmap export" -thread here

  • Granular PNG Export Options here

  • 'Feature Request - Export Layers to PNG sequence…' (GUI, mostly) here

  • 'Selectively Export layers from Command Line Interface' - already doable AFAIU with '--export-id' here

Solution candidates

  • JessyInk: "extension bundled with Inkscape to create a layers-based presentation, which includes an output format (via 'Save') called 'JessInk zipped pdf or png putput (.zip)"*. It "can create a zip archive optionally with a PDF or PNG per layer". More here.

  • "[E]xport by id from the command line" but there are some problems such as extra-work in hiding the other layers "if the sprites are all on top of each other". A sample shell script to export each layer to png here.

  • "[D]efine the export areas on the command line", more here.

  • [Novel idea] "another external extension (designed to create animations) also can batch export each layer to bitmap", more here.

References

  1. Inkscape manual about exportation here.

  2. Command line -usage instructions with $ inkscape --usage.

2

Well, sorry for resuscitating a thread whose solution was already accepted.

I was facing the same "batch layer exporting" problem, except I wanted to export groups of layers together, for instance LAYER0+LAYER1, LAYER0+LAYER2, LAYER1+LAYER2. Neither JessyInk nor the script files from Inkscape Launchpad worked for me...

This is one of the first answers I read and it gave me some precious links for the custom lightweight script I wrote, that I now share with you guys, hoping it will be useful: https://sites.google.com/site/rameyarnaud/media/images/inkscape-export-svg-layers-to-gif-animation

Cheers!

1

I think Inkscape has no such option. A workaround is to use the chain ExportOverlays + Ghostscript.

  1. ExportOverlays exports n Inkscape layers into n pdf files. More here.

  2. Then with ghostscript, you convert each pdf into a bitmap. More here.

This does "batch export layers into bitmaps" on a Linux system. I don't know the how-to for Mac and Windows.

0
1

As of Inkscape 1.2+ there is now a native batch export functionality.

You still access it from the File > Export dialog with ⇧ Shift + Ctrl + E as usual, but now there is a Batch tab to export in mass.

From here you can batch export selections of layers, multiple pages or selected objects into raster (or even vector) formats. You can also add multiple simultaneous formats to the export list, like say JPEG and PNG.

All formats previously supported for Save-As are now made available for batch export as well.

enter image description here

To take full advantage of this functionality you may have to enable it in Edit > Preferences > Input/Output > Show all outputs in Export Dialog.

0

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

Objects to export can be identified by some regular expression on the id, or using xpath. Any option from synfig's command line can be passed to the script, including the specification of your area to export (either --export-area-page or--export-area=x0:y0:x1:y1)

result command would be something like

./svg-objects-export.py --xpath "//svg:g[@inkscape:groupmode='layer']" --extra '--export-area=40:80:340:580 --export-id-only' -d output-dir/ input.svg

Hope this helps.

0

I've found a bash one-liner that can export all layers to separate files, maintaining page size:

for i in $(seq 1 12); do inkscape -f input.svg -z -C -i "layer$i" -j -h 1080 -e "$i.png" & done

This will loop over layers 1 - 12 and render each to a separate PNG file name layerN.png

0

I found this neat python code to take an input.svg file, determine which layers are shown and which are hidden, and export the result to a separate output.svg file:

exportlayers.py --hide layerA --show layerB --show layerB input.svg output.svg

This is quite convenient to programmatically make animations for inclusion in LaTeX beamer presentation. Each "output.svg" file is easily converted to pdf from the command line with inkscape.

inkscape output.svg --export-pdf=output.pdf

Hope this helps.

0

I've written a bona-fide Inkscape extension for scenarios like this. I call it the SLiCk Layer Combinator. You can use it from within Inkscape, or as part of a script if you need to run other operations on the output files such as conversion to PNG.

If all you need is to output one layer per file -- without combining those layers with other layers (which Slick can also do) -- then you would create a single parent layer called something like --frames-- and then Slick will automatically create a separate Inkscape SVG file for each layer under --frames-- when run.

https://github.com/juanitogan/slick

Your Answer

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

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