Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PNG with alpha-transparency to SVG with potrace

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 !

like image 616
Lolo Avatar asked Sep 25 '15 09:09

Lolo


1 Answers

@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
like image 153
Paul Avatar answered Nov 15 '22 08:11

Paul