如何将PNG图像转换为SVG?


当前回答

注意那些使用potrace和imagemagick的人,将透明的PNG图像转换为PPM似乎不是很好。下面是一个在convert上使用-flatten标志来处理这个问题的例子:

sudo apt-get install potrace imagemagick
convert -flatten input.png output.ppm
potrace -s output.ppm -o output.svg
rm output.ppm

Another interesting phenomenon is that you can use PPM (256*3 colors, ie. RGB), PGM (256 colors, ie. grayscale) or PBM (2 colors, ie. white or black only) as the input format. From my limited observations, it would appear that on images which are anti-aliased, PPM and PGM (which produce identical SVGs as far as I can see) shrink the colored area and PBM expands the colored area (albeit only a little). Presumably this is the difference between a pixel > (256 / 2) test and a pixel > 0 test. You can switch between the three by changing the file extension, ie. the following use PBM:

sudo apt-get install potrace imagemagick
convert -flatten input.png output.pbm
potrace -s output.pbm -o output.svg
rm output.pbm

其他回答

potrace不支持PNG作为输入文件,而是支持PNM。 因此,首先将PNG转换为PNM:

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

解释选项

potrace -s =>输出文件是SVG potrace -o file.svg =>将输出写入file.svg

例子

输入文件= 2017.png

convert 2017.png 2017.pnm

临时文件= 2017.pnm

potrace 2017.pnm -s -o 2017.svg

输出文件= 2017.svg

脚本

ykarikos提出了一个脚本png2svg.sh,我已经改进了:

#!/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

一行命令

如果你想转换很多文件,你也可以使用下面的单行命令:

( 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 )

另请参阅

也可以在维基百科上看到栅格转换器和矢量转换器的比较。

有一个网站,你可以上传你的图片,并看到结果。

http://vectormagic.com/home

但是如果你想下载你的svg-image,你需要注册。 (注册即可免费获得2张图片)

令我惊讶的是,potrace原来只能处理黑色和白色。对于您的用例来说,这可能没问题,但有些人可能会认为缺乏颜色追踪是有问题的。

就我个人而言,我对 向量的魔法

但它仍然不完美。

png是位图图像风格,SVG是支持位图的基于矢量的图形设计,因此它不会将图像转换为矢量,只是将图像嵌入到基于矢量的格式中。你可以使用免费的http://www.inkscape.org/来实现。它会嵌入它,但是它也有一个Live Trace之类的引擎,如果你愿意,它会尝试将它转换为路径(使用potrace)。查看adobe illustrator中的实时跟踪(商业)是一个例子:

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

这个工具现在工作得很好。

http://www.mobilefish.com/services/image2svg/image2svg.php