266

How to convert a PNG image to a SVG?

Termininja
  • 6,620
  • 12
  • 48
  • 49
user199337
  • 8,063
  • 7
  • 23
  • 18
  • Through some application or through Operating system commands ? If through Operating system could you please tell which operating system.. Thanks – Mahesh Velaga Dec 07 '09 at 17:10

15 Answers15

130

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)

Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
FrankkieNL
  • 1,540
  • 1
  • 9
  • 2
122

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.

Community
  • 1
  • 1
oHo
  • 51,447
  • 27
  • 165
  • 200
  • 2
    The image looses color when converted from pnm to svg using potrace. Is there any way to preserve color? – Coder Oct 09 '14 at 01:47
  • 1
    Hi @mundella. I have always used `potrace` for black & white icons. Thanks for your feedback. But sorry no idea how to preserve colors... Ho! I have an idea: If your original image has few colors (let's say three unique colors), you may create three initial images (one for each color). Then convert to SVG. And finally, merge the three SVG content in one file (SVG is XML based). Hope this could help... Cheers ;-) – oHo Oct 09 '14 at 19:21
  • @olibre Is there any platform of pragmatically do the same things – Ami Kamboj Jul 29 '16 at 08:08
  • 1
    Hi @AmiKamboj. I am not sure to understand your question. The commands `convert` and `potrace` are available on many platforms. I am using Linux-based computers. I have just used these commands yesterday to produce a SVG from a scanned paper. You can see the result : http://cpp-frug.github.io/images/Cpp-President-2017.svg Please explain more about your target. What do you want / wonder ? What is your need / wish ? Cheers ;-) – oHo Jul 29 '16 at 08:41
  • @olibre, Thankyou for your valuable comments. I am new in this technique.Is it possible to connect with me on skype please. Thankyou (S : -amitkambo) – Ami Kamboj Jul 29 '16 at 09:23
  • Hi @AmiKamboj I do not have Skype. But I have extended my answer with an example providing input and output images. Hope this is fine for your requirements. Have fun, cheers ;-) – oHo Aug 07 '16 at 21:58
  • 1
    I've used just about every online x to svg converter out there. The potrace npm package blew them all out of the water. It's awesome. – tsteve Nov 18 '17 at 20:42
  • convert or pngtopng gave me not antialiased edges. I needed to open in Gimp, change Image > Mode > RGB, change light grey to black, export as ppm. – rofrol Apr 05 '18 at 19:01
59

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

Keith Adler
  • 20,880
  • 28
  • 119
  • 189
  • 1
    I've used the live trace in Inkscape to re-create logos with long lost originals. Unless the latest version has made some improvements, it is a bit hit-or-miss. Having said that, I've also manually traced some logos in Inkscape and managed quite fine. – AnonJr Dec 07 '09 at 17:19
  • 12
    Here is an explanation how to do this in Inkscape: http://wiki.inkscape.org/wiki/index.php/Potrace – Erel Segal-Halevi Oct 09 '12 at 06:43
  • great comment. We have noticed that iOS 7.1 Safari will not display images embedded in svg's while Chrome does not have a problem with it – ProblemsOfSumit Sep 03 '14 at 10:00
28

You may want to look at potrace.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • 1
    this Potrace is very accurate when you know how to use it: but its not multicolor 1.) convert your file to BMP 2.) (for the accurate part) convert bmp to a high resolution(strech it) 3.) color the shapes you want to trace in black 4.) convert 5.) change width and height to original 6.) appreciate the accurate tracing. – PauAI Dec 14 '15 at 02:28
18

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.

Pancho
  • 2,043
  • 24
  • 39
  • 1
    Why the downvote? I use this approach very successfully. At least have the courtesy to explain why so people can benefit from your insights. – Pancho Feb 05 '16 at 16:37
  • 5
    Following those instructions only wraps the image with SVG XML - it doesn't convert anything to path data. – Robert Feb 06 '16 at 19:25
  • @Robert - thanks for the info, I was blissfully ignorant as it worked fine for my purposes. This does not however detract from the fact that Inkscape is able to perform a complete PNG to SVG conversion - and much more. To illustrate for anyone interested I amended my answer to include a short explanatory video clip. – Pancho Feb 06 '16 at 23:24
  • Is that really so? I checked the generated SVG and it appears to have path data, unlike the mobilefish method... – dario_ramos Apr 23 '17 at 07:16
  • This is the easiest way. Should be the top answer. All steps are explained in the video. And it's a real SVG, nothing embedded – Andreas M. Oberheim Feb 10 '18 at 18:04
15

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

JayD
  • 6,173
  • 4
  • 20
  • 24
  • 3
    +1 for online-convert.com. Tried it now with a couple of monochrome silhouettes.. performed flawlessly.. Also seems to be completely free. – nedR Dec 08 '13 at 13:24
  • Yea one of the better converters... Thanks – JayD Dec 09 '14 at 22:52
  • 1
    Didn't realize Illustrator had this cooked in. Having everything as .ai already, this will save me a ton of time. – Arielle Lewis Feb 04 '15 at 22:50
10

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.

cavalcade
  • 1,367
  • 14
  • 15
9

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
P̲̳x͓L̳
  • 3,615
  • 3
  • 29
  • 37
Bardi Harborow
  • 1,803
  • 1
  • 28
  • 41
7

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

I always use it for my needs.

thednp
  • 4,401
  • 4
  • 33
  • 45
  • I really works like a charm even in comlex images. I really advice this tool. – Atıfcan Ergin Jan 07 '16 at 16:16
  • Didn't work for me unfortunately. – adamj Apr 13 '16 at 05:55
  • Not work well if the image have transparency – Genaut Apr 13 '16 at 11:38
  • You cannot expect the script to take a photo of yourself and get a perfectly layered SVG out as a result, there is no such a thing. If you have a simple shape and you want to get the path(s), this does it. I made alot of font-icons with that page and never failed for me. – thednp Apr 13 '16 at 19:29
  • @thednp my image came out black and white – iOSAndroidWindowsMobileAppsDev Feb 01 '17 at 11:51
  • @Anon You can play with the options a bit, I am generally interested in getting the shapes, as the fill and stuff is added later via JS or PHP in my case. I am not the one to develop the utility, you might want to head over to the source for support. – thednp Feb 01 '17 at 16:37
  • identical answer as [the one posted 3 months earlier](https://stackoverflow.com/a/25325993); same problem – ssc Jul 11 '17 at 12:37
7

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

Dai Bok
  • 3,451
  • 2
  • 53
  • 70
4

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.

Brian D
  • 2,570
  • 1
  • 24
  • 43
2

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.

monksy
  • 14,156
  • 17
  • 75
  • 124
2

http://online-converting.com/image/convert-to-svg/ worked well for converting to svg

eagor
  • 9,150
  • 8
  • 47
  • 50
  • this removes any color from the image, same problem as with `potrace` on the command line - makes me wonder what the website uses internally... – ssc Jul 11 '17 at 12:36
0

This tool is working very well right now.

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

alejandro
  • 389
  • 2
  • 13
  • 17
    This embeds the image in svg, not real vector graphics – Paul Gobée Sep 08 '14 at 20:34
  • This converted one of my colored pngs in which other online converters were not able to convert properly. I thought it was working since I can open it in browser but when I tried opening it on another tool, it has an error. Maybe because of the comment stated above. – vida May 16 '16 at 00:41
-1

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.

DarkFox
  • 131
  • 3
  • 2
    Imagemagick works on other platforms, not just 'Linux'. – Lunatik Jul 24 '12 at 12:32
  • Thanks for the note- I guess I meant back then that's the only platform that I know it works on- w\ official builds. Go figure it'll be on BSD and there-go mac. Not sure about Microsoft versions as I don't use them- I don't have any need nor interest to search. – DarkFox Sep 03 '12 at 11:23
  • 25
    This technically works, however, the generated svg file is not *really* vector graphics - it just contains the original bitmap "as is". Thus, when you enlarge it, you still get the degraded quality of a raster image. – Erel Segal-Halevi Oct 09 '12 at 06:41
  • @ErelSegalHalevi It works for getting the filetype.. Sure it could be cleaned up to a real vector graphic, I don't use svg personally and never needed to convert one.. I'm sure there are vector tools to clean up the svg and "compress" it into a correct vector. – DarkFox Nov 15 '12 at 09:32
  • 7
    @archeyDevil no, that's not "converting" to SVG, that's "embedding" the bitmap inside an empty SVG. No use to anyone. – Adam Jan 30 '13 at 11:13
  • @Adam I said that already in my last comment. I did mention that there are tools that could compress the embedded bitmap into a correct vector. – DarkFox Jan 31 '13 at 08:57
  • 4
    @archeyDevil Question title is "convert ... to SVG". Your answer doesn't convert. It embeds. PNG is a bitmap format, SVG is a vector format. Massive difference. Embedding is useless and not worthy of an SO question IMHO – Adam Jan 31 '13 at 15:08
  • 3
    I am aware it doesn't convert. I did however; post this for those who don't need a vector persay, but just need a quick and dirty change between filetypes. – DarkFox Feb 01 '13 at 13:09
  • For the record, according to http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=20559, ImageMagick _will_ perform a true trace of the rasterized image if you have the autotracer library installed at the time of compilation. – jeffcook2150 Mar 01 '14 at 20:18
  • @DarkFox still not relevant – afr0 Nov 29 '16 at 02:28