I have many gray scale bitmaps that I need to convert to a vector format, such as SVG. I have been using Inkscape, which gives nice results, but this takes about 10-15 minutes to open, convert, and save a file. Is there a batch or command line tool that can convert these?
5 Answers
potrace
I found this example on SO in a Q&A titled: How to convert a JPEG image into SVG format using ImageMagick?. One of the answers suggested potrace
.
$ convert input.jpg output.ppm
$ potrace -s output.ppm -o svgout.svg
Results
Looking at the potrace
website you can apparently go directly from BMP to SVG.
$ potrace -s input.bmp -o output.svg
autotrace
Another option to potrace
is autotrace
.
$ autotrace -output-file ouput.svg -output-format svg --color-count 4 input.bmp
You'll likely have to play with the --color-count
to get an image that suites your needs.
Results
Which to use?
Of these 2 it really depends on the source material. If you're converting images then potrace
is likely the better option. For non-photo material such as fonts and inanimate objects such as scenery that includes things like roads, then autotrace
with its --centerline
switch.
While not optimal for photos, it does good job on line art and poster-like images. Doesn't do too well with color. Autotrace has some features not available in the embedded Potrace dialog in Inkscape. For example, there is a centerline switch that is especially usefull for tracing roads (in maps) or font characters.
Batch conversions
You can wrap either of these commands in a for loop in Bash, which will make easy work of doing bulk conversion tasks.
$ for i in *.bmp; do <CONVERSION TOOL> ... "$i"; done
References
-
there's no need to store an intermediate file from the JPEG. as a one-liner I used:
convert -channel RGB -compress None input.jpg bmp:- | potrace -s - -o output.svg
Mar 17, 2016 at 17:50
Autotrace
You could try autotrace.
Using the following command you get the following results:
autotrace --output-format svg --output-file output.svg --color-count 4 imgsrc.jpg
I had to take a screencapture of the resulting svg and save as png to show the output.
Here is a source image jpg:
Here is the resulting image:
Normally, I do all my image manipulation tasks with convert
from ImageMagic but I can't get it to play nice with svg files. You can, however, use inkscape
itself from the command line:
for i in *bmp; do inkscape -f "$i" -l "$i.svg"; done
That will create files called foo.bmp.svg
. To get the names right, try this:
for i in *bmp; do inkscape -f "$i" -l "${i%.bmp}.svg"; done
The problem with this approach is that, at least on my system, it pops up an annoying graphical dialog asking if I want to link or embed the image:
So, you still need to click OK manually, but it is still much much faster than doing it file by file.
The output images are identical as far as I can tell:
Theoretically, both convert
and rsvg-convert
should be able to do this but I couldn't get it to work well on my images in the few minutes I spent trying. They're both worth looking into nevertheless.
-
I'm not positive but on the Inkscape website it says that it now has
potrace
embedded, so I wonder if it's just using that tool anyway? quote: "Potrace is now embedded into Inkscape. You don't need to run it separately." -- wiki.inkscape.org/wiki/index.php/Tools– slm ♦Jan 25, 2014 at 2:12 -
7I suspect Inkscape will simply embed the bitmap image inside the SVG, hence their identical look. Jan 25, 2014 at 6:51
-
@user2428118 I suspect you're right. That may or not be a problem depending on what exactly you want to do with these images.– terdon ♦Jan 25, 2014 at 17:42
Here is a wiki comparsion of all Bitmap-to-SVG tools. I recommend two tools based on my own testing. Both of them can convert bitmaps to SVGs in a batch manner.
imagetracerjs is a free js/java tool, which can vertorize the bitmaps while keeping colors.
vectormagic is a commercial tool and it outperforms other tools in my testing.
potrace
is the tool for that. You can process them bulk, but first you have to convert the images to monochrome:
ls ./*.jpg | xargs -L1 -I {} convert -monochrome {} {}.ppm
ls -1 ./*.ppm | xargs -L1 -I {} potrace {} -r 300 -b svg -o {}.svg
Install on Ubuntu:
sudo apt install potrace
If you want to convert images that are not monochrome, Inkscape
has a tracer tool base on potrace that support multiple colors, it first converts the image to color layers and then it trace those and combines the result.
To use the tracer, load or import an image, select it, and select the Path > Trace Bitmap item, or Shift+Alt+B.
See
potrace
as well.