How to convert a PNG image to a SVG?

There is a website where you can upload your image, and see the result.

http://vectormagic.com/home

But if you want to download your svg-image, you need to register. (If you register, you get 2 images for free)


potrace does not support PNG as input file, but PNM.
Therefore, first convert from PNG to PNM:

convert file.png file.pnm        # PNG to PNM
potrace file.pnm -s -o file.svg  # PNM to SVG

Explain options

  • potrace -s => Output file is SVG
  • potrace -o file.svg => Write output to file.svg

Example

Input file = 2017.png Input file

convert 2017.png 2017.pnm

Temporary file = 2017.pnm

potrace 2017.pnm -s -o 2017.svg

Output file = 2017.svg Output file

Script

ykarikos proposes a script png2svg.sh that I have improved:

#!/bin/bash

File_png="${1?:Usage: $0 file.png}"

if [[ ! -s "$File_png" ]]; then
  echo >&2 "The first argument ($File_png)"
  echo >&2 "must be a file having a size greater than zero"
  ( set -x ; ls -s "$File_png" )
  exit 1
fi

File="${File_png%.*}"

convert "$File_png" "$File.pnm"        # PNG to PNM
potrace "$File.pnm" -s -o "$File.svg"  # PNM to SVG
rm "$File.pnm"                         # Remove PNM

One-line command

If you want to convert many files, you can also use the following one-line command:

( set -x ; for f_png in *.png ; do f="${f_png%.png}" ; convert "$f_png" "$f.pnm" && potrace "$f.pnm" -s -o "$f.svg" ; done )

See also

See also this good comparison of raster to vector converters on Wikipedia.


A png is a bitmap image style and an SVG is a vector-based graphics design which supports bitmaps so it's not as if it would convert the image to vectors, just an image embedded in a vector-based format. You could do this using http://www.inkscape.org/ which is free. It would embed it, however it also has a Live Trace like engine which will try to convert it to paths if you wish (using potrace). See live trace in adobe illustrator (commericial) is an example:

http://graphicssoft.about.com/od/illustrator/ss/sflivetrace.htm

Tags:

Png

Svg