5

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 : https://i.stack.imgur.com/aeUay.png

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

Thanks !

Gonzalo Matheu
  • 8,984
  • 5
  • 35
  • 58
Lolo
  • 61
  • 1
  • 4
  • 1
    Removing the transparency should work, but maybe you're doing it wrong for a reason or another. You could have a look at this: http://www.riverrockapps.com/post/convert-white-transparent-png-to-svg – philippe_b Oct 06 '15 at 09:39
  • 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 Oct 12 '15 at 07:15
  • 1
    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 Oct 12 '15 at 07:25

2 Answers2

6

@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
Paul
  • 527
  • 5
  • 17
0

@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
Uschi
  • 11
  • 3