如何将 PNG 图像转换为 SVG?

发布于 2024-08-14 00:46:22 字数 25 浏览 7 评论 0 原文

如何将 PNG 图像转换为 SVG?

How to convert a PNG image to a SVG?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

嘿咻 2024-08-21 00:46:22

有一个网站,您可以上传图像并查看结果。

http://vectormagic.com/home

但如果您想下载 svg 图像,则需要注册。
(注册后可免费获得2张图片)

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)

从此见与不见 2024-08-21 00:46:22

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 输出文件

脚本

< a href="https://gist.github.com/ykarikos" rel="noreferrer">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 )

另请参见

维基百科上栅格与矢量转换器的良好比较

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.

横笛休吹塞上声 2024-08-21 00:46:22

png 是一种位图图像样式,而 SVG 是一种基于矢量的图形设计,支持位图,因此它不会将图像转换为矢量,而只是嵌入在基于矢量的格式中的图像。您可以使用免费的 http://www.inkscape.org/ 来完成此操作。它会嵌入它,但它也有一个类似 Live Trace 的引擎,如果您愿意,它会尝试将其转换为路径(使用 potrace)。请参阅 adobe illustrator(商业)中的实时跟踪示例:

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

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

方圜几里 2024-08-21 00:46:22

您可能需要查看 potrace

You may want to look at potrace.

樱花细雨 2024-08-21 00:46:22

轻松

  1. 下载Inkscape(完全免费)
  2. 按照这个简短的说明进行操作youtube video

如您所见,如果您想做很多其他巧妙的 .svg 内容,您也可以使用 Inkscape 来完成。

一个非技术性的观察:我个人更喜欢这种方法而不是“免费”网站产品,因为除了经常需要注册之外,通过上传图像,在所有实践中,人们都将图像提供给网站所有者。

Easy

  1. Download Inkscape (it's completely free)
  2. Follow the instructions in this short youtube video

As you'll see, if you want to do a whole lot of other clever .svg stuff you can do it using Inkscape also.

A non-technical observation: I personally prefer this method over "free" website offerings because, aside from often requiring registration, by uploading the image, in all practical terms, one is giving the image to the website owner.

夏尔 2024-08-21 00:46:22

使用 adobe illustrator:

打开 Adob​​e Illustrator。单击“文件”并选择“打开”将 .PNG 文件加载到程序中。根据需要编辑图像,然后将其保存为 .SVG 文件。单击“文件”并选择“另存为”。创建新文件名或使用现有名称。确保所选文件类型是 SVG。选择一个目录并单击“保存”以保存文件。

在线转换器
http://image.online-convert.com/convert-to-svg

我更喜欢人工智能,因为你可以做任何需要的改变

祝你好运

with adobe illustrator:

Open Adobe Illustrator. Click "File" and select "Open" to load the .PNG file into the program.Edit the image as needed before saving it as a .SVG file. Click "File" and select "Save As." Create a new file name or use the existing name. Make sure the selected file type is SVG. Choose a directory and click "Save" to save the file.

or

online converter
http://image.online-convert.com/convert-to-svg

i prefer AI because you can make any changes needed

good luck

小嗷兮 2024-08-21 00:46:22

令我惊讶的是,potrace 事实证明,只能处理黑色和白色的。这可能适合您的用例,但有些人可能会认为缺乏颜色跟踪是有问题的。

就我个人而言,我已经取得了满意的结果
Vector Magic

但它并不完美。

To my surprise, potrace it turns out, can only process black and white. That may be fine for you use case, but some may consider lack of color tracing to be problematic.

Personally, I've had satisfactory results with
Vector Magic

Still it's not perfect.

提赋 2024-08-21 00:46:22

给那些使用 potraceimagemagick,将透明的 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

另一个有趣的现象是您可以使用 PPM(256*3 色,即 RGB),PGM(256 色,即灰度)或 PBM(2颜色,即仅白色或黑色)作为输入格式。根据我有限的观察,在抗锯齿图像上,PPMPGM (产生相同的 SVG)缩小彩色区域和 PBM 扩大了彩色区域(尽管只有一点点)。据推测,这就是 pixel > 之间的差异。 (256 / 2) 测试和 pixel > 0 测试。您可以通过更改文件扩展名在这三者之间切换,即。以下使用PBM

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

A note to those using potrace and imagemagick, converting PNG images with transparency to PPM doesn't seem to work very well. Here is an example that uses the -flatten flag on convert to handle this:

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
北城半夏 2024-08-21 00:46:22

您还可以尝试 http://image.online-convert.com/convert-to-svg

我总是用它来满足我的需要。

You can also try http://image.online-convert.com/convert-to-svg

I always use it for my needs.

花落人断肠 2024-08-21 00:46:22

我刚刚发现这个问题和答案,因为我正在尝试做同样的事情!我不想使用提到的其他一些工具。 (不想泄露我的电子邮件,也不想付费)。我发现 Inkscape (v0.91) 可以做得很好。本教程快速且易于理解。

只需在 Inkskape 中选择位图并 Shift+Alt+B 即可。

使用 Inksape Trace 进行边缘检测基于potrace的位图工具

I just found this question and answers as I am trying to do the same thing! I did not want to use some of the other tools mentioned. (Don't want to give my email away, and don't want to pay). I found that Inkscape (v0.91) can do a pretty good job. This tutorial is quick to and easy to understand.

Its as simple as selecting your bitmap in Inkskape and Shift+Alt+B.

Edge Detection with Inksape Trace bitmap tool based on potrace

挽容 2024-08-21 00:46:22

根据您想要从 .png 转换为 .svg 的原因,您可能不必经历这个麻烦。如果您不太熟悉可用的工具,或者您不是图形设计师,那么从 .png(光栅)转换为 .svg(矢量)可能会很痛苦。

如果有人向您发送了一个大的高分辨率文件(例如 1024x1024),您可以在 GIMP 中将其大小调整为您想要的任何大小。通常,如果分辨率(每英寸像素数)太低,则在调整图像大小时会遇到问题。要在 GIMP 中纠正此问题,您可以:

  1. File ->打开:您的.png文件
  2. Image ->图像属性:检查分辨率和色彩空间。您需要 300 ppi 左右的分辨率。在大多数情况下,您希望颜色空间为 RGB。
  3. 图像 ->模式:设置为 RGB
  4. Image ->缩放图像:保留大小,将 Y 分辨率设置为 300 或更大。点击规模。
  5. 图像 ->缩放图像:分辨率现在应为 300,您现在可以将图像大小调整为几乎任何您想要的尺寸。

虽然不像调整 .svg 文件大小那么容易,但如果您已经有一个大的高分辨率图像,那么绝对比尝试将 .png 转换为 .svg 更容易、更快。

Depending on why you want to convert from .png to .svg, you may not have to go through the trouble. Converting from .png (raster) to .svg (vector) can be a pain if you are not very familiar with the tools available, or if you are not a graphic designer by trade.

If someone sends you a large, high resolution file (e.g. 1024x1024), you can resize that down to pretty much any size you want in GIMP. Often, you will have problems resizing an image if the resolution (number of pixels per inch) is too low. To rectify this in GIMP, you can:

  1. File -> Open: your .png file
  2. Image -> Image Properties: check the Resolution, and the color space. You want a resolution around 300 ppi. In most cases you want the color space to be RGB.
  3. Image -> Mode: set to RGB
  4. Image -> Scale Image: leave the size alone, set and Y resolution to 300 or greater. Hit Scale.
  5. Image -> Scale Image: the resolution should now be 300 and you can now resize the image down to pretty much any size you want.

Not as easy as resizing a .svg file, but definitely easier and faster than trying to convert a .png to a .svg, if you already have a big, high-resolution image.

感悟人生的甜 2024-08-21 00:46:22

我假设您希望编写软件来执行此操作。天真地做到这一点,你只需找到直线并设置向量即可。为了智能地做到这一点,您尝试将形状拟合到绘图上(模型拟合)。此外,您应该尝试确定位图区域(无法通过羞耻或应用纹理进行建模的区域)。我不建议采用这条路线,因为这将花费相当多的时间,并且需要一些图形和计算机视觉知识。但是,输出将比原始输出好得多,而且规模也好得多。

I'm assuming that you wish to write software to do this. To do it naively you would just find lines and set the vectors. To do it intelligently, you attempt to fit shapes onto the drawing (model fitting). Additionally, you should attempt to ascertain bitmaped regions (regions you can't model through shames or applying textures. I would not recommend going this route as that it will take quite a bit of time and require a bit of graphics and computer vision knowledge. However, the output will much and scale much better than your original output.

随波逐流 2024-08-21 00:46:22

如果您使用的是 Linux 系统,imagemagick 是完美的选择。即

convert somefile.png somefile.svg

这适用于不同格式的堆。

用于其他媒体,例如视频和音频使用(ffmpeg)
我知道你清楚地说明了 png 到 svg,但是;它仍然与媒体相关。

ffmpeg -i somefile.mp3 somefile.ogg

如果您想浏览大量文件,这只是一个提示;使用基本 shell 技巧的循环..

for f in *.jpg; do convert $f ${f%jpg}png; done

删除 jpg 并添加 png 告诉转换你想要什么。

If you're on some Linux system, imagemagick is perfect. I.e

convert somefile.png somefile.svg

This works with heaps of different formats.

For other media such as videos and audio use (ffmpeg)
I know you clearly stated png to svg, however; It's still media related.

ffmpeg -i somefile.mp3 somefile.ogg

Just a tip for if you wish to go through lots of files; a loop using basic shell tricks..

for f in *.jpg; do convert $f ${f%jpg}png; done

That removes the jpg and adds png which tells convert what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文