How to convert a PNG image to a SVG?

如何将PNG图像转换为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 S VG
  • potrace -o file.svg => Write output to file.svg
  • Example

    Input file = 2017.png 输入文件

    convert 2017.png 2017.pnm
    

    Temporary file = 2017.pnm

    potrace 2017.pnm -s -o 2017.svg
    

    Output file = 2017.svg

    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

    链接地址: http://www.djcxy.com/p/77324.html

    上一篇: 在浏览器中将SVG转换为图像(JPEG,PNG等)

    下一篇: 如何将PNG图像转换为SVG?