5
votes

I'm using potrace to convert a png file to svg. The png is black on transparent background with alpha-transparency levels. I would like to report them in the svg output. Is it possible ? Potrace skips the alpha-transparency and turns it to black.

Here is my command:

convert -alpha Remove file.png pgm: | potrace --svg -o file.svg

PNG : http://i.imgur.com/d2ZYrf6.png

SVG output (.svg in reality but you can see directly the result in png) : http://i.imgur.com/n1NsNYQ.png

Thanks !

2
Removing the transparency should work, but maybe you're doing it wrong for a reason or another. You could have a look at this: riverrockapps.com/post/convert-white-transparent-png-to-svgphilippe_b
Thanks philippe_b. I had a look on this page before but there's no alpha-transparency levels, it's only full white (or black) png on transparent background.Lolo
I tried to convert your picture, just to see what happens. I think I now understand the issue: all non-white regions become black, whereas you expect black (for the "foreground man") and grey (for the two "background men"). What I initially got with my own picture was a full-black SVG, so this was not the same issue after all. Did you try different bitmap formats? I get something different with the PBM format, although this is not what you want.philippe_b

2 Answers

6
votes

@Lolo. I don't think a pipeline with potrace alone can do what you want. From the manpage:

The potrace algorithm expects a bitmap, thus all pixels of the input images are converted to black or white before processing begins.

@philippe_b. I ran into the same problem as you, i.e.

convert foo.png foo.pbm && potrace foo.pbm -s -o foo.svg

gave me an all black output image. Incidentally, this was actually happening at the PNG->PBM stage. My images had transparent alpha. This is my working solution

pngtopnm -mix assign.png > a.pnm && potrace a.pnm -s -o a.svg

Here's a script to do it in batches

#!/bin/bash

for src in *.png; do
    name=`basename $src .png`
    pnm="$name.pnm"
    svg="$name.svg"
    pngtopnm -mix $src > $pnm && potrace $pnm -s -o $svg && rm $pnm
    # set colour
    # sed -i "s/#000000/#016b8f/g" *.svg
    # same for PNG
    # mogrify -fill '#016b8f' -opaque black *.png
done
0
votes

@paul did a good job and it works for me. But you can shorten it again to not write unnecessary files to the disk, similarly to @Lolo:

pngtopnm -mix inputfile.png | potrace --svg -o outputfile.svg