Search code examples
svgscriptingvector-graphicsinkscapeimage-conversion

Is it possible to make Inkscape autotrace PNG to SVG, but from the command line?


I want to automate "raster to vector" conversions. PNG to SVG. (most Qs here on SO are the other way around)

I have tried the old command line tool autotrace on Linux, but I could not get it to run. I've tried to install a package, and to compile it from the source. Nope.

Then I've realised that Inkscape has "autotrace" now integrated in its codebase. I'd like to convert simple sketches from PNG to SVG.

And I want to do this in a Bash for-loop , with different autotrace settings (number of passes; ignore Speckles with max X pixels width) etc.

I've tried the "action" command-line option

inkscape --without-gui --actions="file-open:my.png"

and this brings up the small "png bitmap image import" dialog, waiting for me to confirm.

Also I've tried the verb command line option

inkscape --with-gui  --verb="FileImport:my.png"

and this opens the large "Select file to import" dialog (ignoring my --verb argument)

At this point I gave up.

I want Inkscape to import a PNG picture, autotrace it with some settings, save it as SVG. Perhaps, beofre saving, duplicate the traced layer, lock the imported background layer, rename the layers from path-12345 to "tracesettings-x-y-z" etc.

(my final goal is to permute the tracing settings, to find good ones for my use-case, but that's not the focus of this question)


Solution

  • Inkscape is using potrace and autotrace to trace bitmap images into vector formats such as SVG and PDF.

    Let's assume you have an image: foo.png that you want to trace to SVG using potrace:

    • First, you need to convert your image to a bitmap format (BMP).
    • Invoke the potrace command
    # I am using ImageMagick convert command to convert PNG to BMP
    convert foo.png foo.bmp
    
    # Invoke potrace command with SVG backend
    potrace -b svg foo.bmp
    

    The result will be: foo.svg.