Examples for Inkscape command line usage wanted

General discussions about Inkscape.
Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Examples for Inkscape command line usage wanted

Postby Moini » Tue Nov 27, 2018 9:04 am

The Inkscape command line is currently undergoing large changes. To better know what users use and do on the Inkscape command line, the developer, Tavmjong Bah, is asking for links to examples of using the Inkscape command line.

Please provide them in a post below, and also add a short description that says what you use it for.

Thank you for helping Inkscape become better!
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

aportale
Posts: 1
Joined: Wed Nov 28, 2018 7:33 am

Re: Examples for Inkscape command line usage wanted

Postby aportale » Wed Nov 28, 2018 7:39 am

Hi, the Qt Creator IDE uses has all its icons done with Inkscape. In order to mass-export the icons to pngs in normal and @2x version, the Inkscape command line interface is called from a Python script: http://code.qt.io/cgit/qt-creator/qt-cr ... ort.py#n85

The used command line parameters are:
--export-id=<id> --export-id-only --export-png=<png> --export-dpi=<dpi>

Bananicorn
Posts: 1
Joined: Wed Nov 28, 2018 7:35 am

Re: Examples for Inkscape command line usage wanted

Postby Bananicorn » Wed Nov 28, 2018 8:04 am

I'm using the inkscape commandline in conjunction with imagemagick, to make spritesheets for my in-game animations.
The source file for the animation is an svg with a bunch of layers, each making up a single animation frame.
While working with the file, I often hide all layers but the one I'm working on, so this is the first thing I do when exporting an animation to png:

Code: Select all

inkscape --verb LayerShowAll --verb FileSave --verb FileQuit $svg_name


I just wish there was a possibility to run this without opening the inkscape gui, since that slows the whole thing down by quite a lot. (I could replace that with a sed script, which would be much more error-prone though)

That's all fine and dandy, but after that comes this monstrosity, which works fine enough:

Code: Select all

for i in {1..17}
do
   layerName=opening_$i
   layerId=$(grep -Pazo "(?s)<[^<]*?(label=\"$layerName\").*?>" $svg_name | sed -rn 's/id="(.*?)"/\1/p') #get layer id from layer name
   #out with the extra spaces
   layerId=${layerId// /}
   inkscape $svg_name -z --export-id=$layerId --export-id-only --export-area-page --export-png=$layerName.png -w$image_width
   convert +append -background transparent flower_sprite_part.png $layerName.png flower_sprite_part.png
   rm $layerName.png
done


In short, I'm getting the Id of the layer through grepping for the layer name, then mangling the result through sed,
then I export said layer and stitch it to the right end of the current line of the spritesheet with imagemagick.

If you feel like my computer should be taken away from me... then I can't blame you.

I've attached the full script and source file for you to have a look at, but it wouldn't let me upload the script as a .sh file or as a .txt file, so I uploaded it with the .svg extension - bash doesn't seem to have a problem with executing it anyways :)

Lastly, thank you so much for all the hard work that already went into this feature (and inkscape in its entirety, I couldn't imagine working without it).
You guys rock!
Attachments
make_flower_sprite.svg
this is no svg, but should be a .sh file
(1.37 KiB) Downloaded 339 times
flower.svg
This is the source file, with each frame of the animation on a layer
(53.51 KiB) Downloaded 337 times

yipdw
Posts: 1
Joined: Wed Nov 28, 2018 11:28 am

Re: Examples for Inkscape command line usage wanted

Postby yipdw » Wed Nov 28, 2018 11:33 am

Hi,

My use of the Inkscape command line is similar to what's done in Qt Creator. I have all icons in an SVG alongside the following script: https://gitlab.peach-bun.com/jortjams/j ... ons.sh#L12

The script uses xmlstarlet to get the IDs of all svg:g and svg:text objects on the icons layer. These IDs are then fed to Inkscape via the export_one function, which is run via parallel xargs. Line 12 is the Inkscape invocation:

Code: Select all

inkscape -z -e ${basename}.tmp -i ${id} -j -d $((96*${mul})) ../icons.svg > /dev/null 2>&1

chris-martin
Posts: 1
Joined: Wed Nov 28, 2018 12:51 pm

Re: Examples for Inkscape command line usage wanted

Postby chris-martin » Wed Nov 28, 2018 1:08 pm

The git repository for our website contains a lot of Nix expressions that look like this:

Code: Select all

runCommand "mapToMaybe.png"
{
    buildInputs = [ inkscape ];
    FONTCONFIG_FILE = makeFontsConf {
        fontDirectories = [ fonts.bitter ];
    };
}
''
  inkscape --without-gui \
    --file="${./courses/functortown/mapToMaybe.svg}" \
    --export-width=1200 --export-png="$out"
''


That way we can keep only the smaller SVG source files in version control, and rasters for the website get generated during the build. This is really the only thing we do with Inkscape's command-line interface: convert SVG to PNG and specify a width or height.

There's one request I have to improve this experience: It would be great if Inkscape could read font files directly, instead of requiring us to build a font cache before running Inkscape. It took me a long time to figure out where in the filesystem Inkscape looks for fonts and to figure out that I needed to use the makeFontsConf function.

inkschaap
Posts: 36
Joined: Fri Oct 28, 2011 11:34 pm

Re: Examples for Inkscape command line usage wanted

Postby inkschaap » Wed Nov 28, 2018 5:16 pm

Simple png export:

Code: Select all

inkscape in.svg --export-png out.png -w 64


Converting a source SVG to a symbolic icon (so it can be used by GTK):

Code: Select all

   inkscape -f temp.svg --verb=EditSelectAll \
      --verb=SelectionUnGroup --verb=StrokeToPath --verb=SelectionUnion \
      --verb=FileVacuum \
      --verb=FileSave --verb=FileClose --verb=FileQuit

andybalaam
Posts: 1
Joined: Wed Nov 28, 2018 7:54 pm

Re: Examples for Inkscape command line usage wanted

Postby andybalaam » Wed Nov 28, 2018 7:57 pm

Rabbit Escape converts SVG images to PNGs using the Inkscape command line in its Makefile like this:

Code: Select all

$(IMAGES32_DEST)/%.png: $(IMAGESSVGGEN_DEST)/%.svg
   @echo ".. Converting from SVG: $@"
@mkdir -p $(IMAGES32_DEST); inkscape $< --export-png=$@ --export-dpi=$(DPI_32) > /dev/null


https://github.com/andybalaam/rabbit-es ... efile#L104

and it also does this as a batch in a python script like this:

Code: Select all

find $SRC -name "*.svg" -print | \
while read F; do
{
    NAME=${F##*/}
    NAME=${NAME%.svg}

    echo "$F --export-png=$DEST/$NAME.png --export-dpi=$3"
}; done | \
inkscape --shell


https://github.com/andybalaam/rabbit-es ... ert-images

Thanks for Inkscape!

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: Examples for Inkscape command line usage wanted

Postby Moini » Thu Nov 29, 2018 1:48 am

Thank you, aportale, Bananicorn, yipdw, chris-martin, inkschaap, andybalaam!

@andibalaam I forwarded your Thank-you to the chat :)

@chris-martin
There's one request I have to improve this experience: It would be great if Inkscape could read font files directly, instead of requiring us to build a font cache before running Inkscape. It took me a long time to figure out where in the filesystem Inkscape looks for fonts and to figure out that I needed to use the makeFontsConf function.


I know that the next version can read files saved into the ~/.config/inkscape/fonts directory (and also from custom directories set in the preferences). However, I do not know if these are pulled into Inkscape via font cache, or via some other means (I suspect it uses the same functionality as previously, though). Then there seems to be new support for including stylesheets - and those can pull fonts from anywhere. I haven't tried this out, though. Please keep your eyes open for the soon-to-be released alpha version of Inkscape 1.0, and check for this behavior, as well as try out the alternative (ask on IRC or on the mailing list if you can't find out how to use it - I don't know yet myself). When it works as you like, than yay! If not, then please create a bug report for your request.

@Bananicorn
I just wish there was a possibility to run this without opening the inkscape gui


Yes, that's a commonly requested change, developers are aware of the issue and are working on the prerequisites for making that possible currently. It might take a while, though, it's a lot of work to do (I think not all of it is very complicated, though, just a lot). Anyone who knows C++ would be very welcome to help with that effort (note that I'm not a C++ dev, so better ask a core dev about any details).

I have forwarded your kind words to the chat :)

----------

More examples are welcome, even if they use the same functionality - duplication helps to also gauge the frequency and variation of command usage!
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

feth
Posts: 1
Joined: Fri Nov 30, 2018 12:48 am

Re: Examples for Inkscape command line usage wanted

Postby feth » Fri Nov 30, 2018 12:53 am

Hello,

I have a very old but still working Makefile that could surely be bettered.

Code: Select all

# (c) Feth Arezki
# No copyright where applicable, wtfpl elsewhere.
#
# Produces flat PNG from SVGs. Uses white as background. Exported area: page.
#
# Usage: 'make'.
#       you can specify a value of 300 DPI with 'make DPI=300'
#
# Requisites:
#       inkscape (no graphic display needed)
#       convert, from imagemagick.
#

DPI=600
SVG=$(wildcard *.svg)
PNGS=$(SVG:.svg=-$(DPI)dpi.png)

all: $(PNGS)
        @echo See $^
.PHONY: all

svgpng=inkscape \
        --without-gui --export-background=white --export-area-page --export-dpi=$1 --file=$2 --export-png=$3

flatten=convert -background white -flatten +matte $1 $2

clean:
        -rm $(PNGS)

.PHONY: clean
.IGNORE: clean

help:
        @echo By default, builds PNGs at 600 dpi by default.
        @echo For instance, call "make DPI=200"

%-$(DPI)dpi.png: %.svg
        $(call svgpng,$(DPI),$<,alpha_$@)
        $(call flatten,alpha_$@, $@)
        -rm alpha_$@


Hope it can be of use.

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: Examples for Inkscape command line usage wanted

Postby Moini » Fri Nov 30, 2018 10:09 am

Hi feth, thank you!
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

sygi
Posts: 5
Joined: Mon Nov 26, 2018 4:53 am

Re: Examples for Inkscape command line usage wanted

Postby sygi » Sun Dec 02, 2018 12:02 am

I have an extension for adding kerf to the cutting of the images. It calls this from commandline (after changing the line's width from python):

Code: Select all

inkscape --select=pathX --verb=StrokeToPath --verb=SelectionUnion --verb=SelectionBreakApart --verb=SelectionUnion current_file.svg --verb=FileSave --verb=FileQuit


where pathX are all the ids of the currently selected (in the GUI) elements.

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: Examples for Inkscape command line usage wanted

Postby Moini » Sun Dec 02, 2018 3:35 am

Thank you, sygi, for sharing this example!
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

vac
Posts: 1
Joined: Fri Feb 06, 2015 6:22 pm

Re: Examples for Inkscape command line usage wanted

Postby vac » Sun Dec 02, 2018 11:05 pm

Hello there!
I'm really happy hearing that Inkscape command line is going to be even better!

I'm using Inkscape for creating icons that are used in my software or web pages.
As an example here is the .bat script that for each SVG in current directory creates multiple .png in following resolutions: 16x16 32x32 64x64 128x128 256x256. Thanks to ImageMagick convert, it combines these pngs into one, multi-resolution .ico

Code: Select all

@ECHO OFF
SETLOCAL
FOR /F %%P in ('dir /B *.svg') DO (
FOR %%H in (16 32 64 128 256) DO (
inkscape -z -h %%H -e %%~nP-%%Hico.png %%P
)
convert *ico.png %%~nP.ico
)
ENDLOCAL
pause

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: Examples for Inkscape command line usage wanted

Postby Moini » Mon Dec 03, 2018 8:25 am

Thank you, vac! And a special Welcome to you as the first Windows user who shared his/her script here (at least it looks like that to me)!
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

thorwil
Posts: 2
Joined: Tue Dec 04, 2018 7:14 pm

Re: Examples for Inkscape command line usage wanted

Postby thorwil » Tue Dec 04, 2018 8:47 pm

Hi! I have been using the Inkscape command line for icons before, as already described here.

Recently, I have been working on a template file and export script for print-on-demand services. The idea is to have artwork in a file with one layer per "shop". Each shop layer has sub-layers per product, with a rectangle or group that has an ID like

Code: Select all

productname_4000x3000
. I go through the file and scale and position each of those in relation to the artwork. The script uses xmlstarlet to extract the IDs from the file and takes them apart to get to width (example: 4000) and height (example: 3000). This way, all the names and sizes are given once, in the SVG.

Inkscape is then called like this:

Code: Select all

inkscape $file --export-id=$id --export-width=$width --export-height=$height --export-png=$export_dir/$name'_'$id.png


There are only 2 complications:

1. I don't want to worry about layer visibility when editing, so I use xmlstarlet to make sure all shop layers are hidden on export:

Code: Select all

xml ed -P -S -L -u "//_:g[@inkscape:label='$shop']/@style" -v display:none $file

This only if necessary, and to be undone after export.

2. One of the services requires 300 dpi (at least support says so, they really should only care about actual pixels, I'd say). Since I'm asking Inkscape to take areas of a specific size and scale them to a specific height and width, it will ignore me asking for DPI on top of that. This is of course reasonable. ImageMagick allows me to set DPI afterwards, editing files in-place:

Code: Select all

mogrify -units PixelsPerInch -density 300 $file


The same shop also wants JPG for many cases. For that, I use ImageMagick's convert. Still, I doubt it would be worth it to extend Inkscape's export to formats other than PNG.

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: Examples for Inkscape command line usage wanted

Postby Moini » Thu Dec 06, 2018 9:39 am

Hi thorwil,

this is interesting, thank you!

Did you know that the Inkscape command line can give you object sizes, and can also show/hide layers?

Extending Inkscape for jpg and other graphics formats would be (and actually is) easy enough. However, developers have decided to not give people a lossy format. There are a couple of third-party export extensions that can export to other formats, though.
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

thorwil
Posts: 2
Joined: Tue Dec 04, 2018 7:14 pm

Re: Examples for Inkscape command line usage wanted

Postby thorwil » Sat Dec 08, 2018 3:37 am

Moini wrote:Did you know that the Inkscape command line can give you object sizes, and can also show/hide layers?


My little research suggests that show/hide layers is only possible via

Code: Select all

--verb
, which brings up the GUI, right?

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: Examples for Inkscape command line usage wanted

Postby Moini » Sat Dec 08, 2018 11:01 am

Yes, not for all verbs, as far as I know, but for many. It's a shortcoming that devs want to work on.
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

User avatar
hellocatfood
Posts: 193
Joined: Fri Aug 29, 2008 8:49 pm
Contact:

Re: Examples for Inkscape command line usage wanted

Postby hellocatfood » Mon Dec 17, 2018 7:05 am

I often use Inkscape on the command line to automate the creation of abstract art: https://www.flickr.com/photos/hellocatfood/8435107390/

Code: Select all

#!/bin/bash

# Converts a png file into a mesh-like structure. Use in a folder full of pngs


for file in *.png

do

# get path of file
path=$( readlink -f "$( dirname "$file" )" )

# get filename minus extension
file1=$(basename "$file")
filename="${file1%.*}"

# convert to bmp because potrace only works with them
convert $file "$path"/"$filename".bmp

# trace the file to svg
potrace "$path"/"$filename".bmp -s -t 250 -o "$path"/"$filename".svg

# messy use of inkscape verbs to create a mesh of objects
inkscape -f "$path"/"$filename".svg --verb EditSelectAll --verb SelectionUnGroup --verb EditSelectAll --verb SelectionBreakApart --verb EditSelectAll --verb SelectionUnGroup --verb org.greygreen.inkscape.effects.extrude.noprefs  --verb EditSelectAll --verb org.inkscape.color.negative.noprefs --verb FileSave --verb FileQuit

sed -i s/"fill:#ffffff"/"fill:none"/g "$path"/"$filename".svg
sed -i s/"stroke:#ffffff"/"stroke:#ff0000"/g "$path"/"$filename".svg

convert -background none "$path"/"$filename".svg -quality 100 "$path"/"$filename"_mesh.png

# cleanup files
rm "$path"/"$filename".bmp

done
I'm on The Web | Flickr | tumblr

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: Examples for Inkscape command line usage wanted

Postby Moini » Mon Dec 17, 2018 8:29 am

@hellocatfood : Thank you for your submission! I'll invite Tav now to have a look at the current list.
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

ronburk
Posts: 32
Joined: Sat Nov 05, 2011 6:21 am

Re: Examples for Inkscape command line usage wanted

Postby ronburk » Wed Feb 20, 2019 6:04 pm

Sort of a peripheral note: I use the command-line to automate image building (image goes through other mods after inkscape builds png) like others. The problem is, it sure feels like the old problem of failing to close stderr properly before exiting upon error is still hanging around. This generically makes all command-line use (perhaps it's a Windows-only issue) painful if there's a problem that one hopes to detect and figure out. Failing to redirect stderr to a file will generally produce no output at all.

Example, my build tool produces output today like:

Code: Select all

inkscape.exe 2>inkscape.err -f "Figures\MooresLawHeat\Page4.svg" --without-gui --export-png "Figures\MooresLawHeat\Page4.png"  -export-dpi=1200 --export-area-page
Deleting 'Figures\MooresLawHeat\Page4.png'
inkscape.exe: Exited with status -1073741819 (0xC0000005) errno=0


OK, so inkscape gave an error or crashed. How to figure out what happened? Well, that's what inkscape.err is for.
With 92.3 (Windows 7), inkscape.err would always be empty. Running the command without redirecting stderr produces no output at all (man, I coulda swore this got fixed a while ago). OK, I noticed I was a tick behind, so updated to 92.4. Now, I reliably (I *think*) can see that inkscape.err contains this:

Code: Select all

** (inkscape.exe:14412): CRITICAL **: 22:23:34.327: void SPObject::requestModified(unsigned int): assertion 'this->document != NULL' failed

Emergency save activated!
Emergency save completed. Inkscape will close now.
If you can reproduce this crash, please file a bug at www.inkscape.org
with a detailed description of the steps leading to the crash, so we can fix it.
** Message: 22:23:34.327: Error: Inkscape encountered an internal error and will close now.

But again, failing to redirect stderr produces the silent exit with no output.

I wonder how many people have tried using the Inkscape command-line and then backed away as soon as it seemed to just mysteriously exit without doing anything. I would rate this one of the higher command-line issues to get right if the goal is to increase its usage and utility. This particular crash doesn't happen if I try to do the exact same export from within the GUI; that's another reason I speculate silent failures would make an unknown number of users give up on the command-line with the feeling that "it's broken" since the GUI doesn't show the same problem.

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: Examples for Inkscape command line usage wanted

Postby Moini » Wed Feb 20, 2019 11:25 pm

Yeah, that was a long-standing Windows issue, we even have an FAQ item describing how to get error output: https://inkscape.org/learn/faq/#inkscape-does-not-start . Short version: use inkscape.com.

We once had debug builds for Windows, but didn't do those any more. There's also an issue on gitlab for it: https://gitlab.com/inkscape/inkscape/issues/81 (those are for helping development, though, not the standard error messages)
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

neodigitechuk
Posts: 1
Joined: Wed May 15, 2019 10:30 pm

Re: Examples for Inkscape command line usage wanted

Postby neodigitechuk » Wed May 15, 2019 10:37 pm

Any update about it?

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: Examples for Inkscape command line usage wanted

Postby Moini » Fri May 17, 2019 9:24 am

About what exactly?
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

User avatar
Lazur URH
Posts: 129
Joined: Sun Feb 10, 2013 10:50 pm

Re: Examples for Inkscape command line usage wanted

Postby Lazur URH » Fri May 17, 2019 4:18 pm

Moini wrote:About what exactly?


Please quote the original message.
That first post made sense to me although I'm a bit suspicious as I had to delete another users posts above who edited theirs to have links to another site. In case that happens, then your question would look seemingly coming from nowhere.


neodigitechuk wrote:Any update about it?


Maybe ask Tavmjong in the live chat at inkscape.org.


Return to “General Discussions”