How do I convert a PNG to SVG, using software?

577

Solution 1

So you are looking for raster to vector graphics converter/tracer. potrace & autotrace both are in Ubuntu repository. Myself I tried potrace before which gave nice results with default options. As I remember, both tools do not support compressed formats as input, only bitmap images.

See Potrace examples

Potrace: utility to transform bitmaps into vector graphics

potrace is a utility for tracing a bitmap, which means, transforming a bitmap into a smooth, scalable image. The input is a bitmap (PBM, PGM, PPM, or BMP format), and the default output is an encapsulated PostScript file (EPS). A typical use is to create EPS files from scanned data, such as company or university logos, handwritten notes, etc. The resulting image is not "jaggy" like a bitmap, but smooth. It can then be rendered at any resolution.

Command:

potrace -s inputfile

AutoTrace: bitmap to vector graphics converter

AutoTrace is a program for converting bitmaps to vector graphics. The aim of the AutoTrace project is the development of a freely-available application similar to CorelTrace or Adobe Streamline. In some aspects it is already better. Originally created as a plugin for the GIMP, AutoTrace is now a standalone program.

Command:

autotrace -output-format svg inputfile

References:

  • man potrace
  • man autotrace

Update

Solution 2

Inkscape has got an awesome auto-tracing tool.

  1. Install Inkscape using sudo apt-get install inkscape
  2. Import your image
  3. Select your image
  4. From the menu bar, select Path > Trace Bitmap Item
  5. Adjust the tracing parameters as needed

Check their tracing tutorial for more information.

Once you are comfortable with the tracing options. You can automate it by using CLI of Inkscape.

Solution 3

Use the convert command in the terminal :

For example:

convert EXAMPLE.png EXAMPLE.svg

Here's some info from the manpage:

convert(1) - Linux man page

Name

convert - convert between image formats as well as resize an image, blur, 
          crop, despeckle, dither, draw on, flip, join, re-sample, and much more.

Synopsis

convert [input-options] input-file [output-options] output-file

Solution 4

I just have used gimp right now with amazing results. I first used potrace but all I got was an image all in black with a lot of distortion. Maybe I didn't used it properly but when I tried with gimp just exporting as "eps" (file_name.eps), it was all done. All I have to do was review it setting the resolution with a value of 300. That's it.

Share:
577

Related videos on Youtube

Bastien Vandamme
Author by

Bastien Vandamme

Updated on September 18, 2022

Comments

  • Bastien Vandamme
    Bastien Vandamme over 1 year

    For this example, I'm using LINQPad and I choose a simple example. For real I'm implementing the solution on a web API.

    Let's say I have a user and a role. For some reason, I would like to check nobody manipulated the user and role at some point of my code.

    Alice is a basic user so:

    var bytes = System.Text.Encoding.UTF8.GetBytes("alice:basicuser");
    string token= Convert.ToBase64String(bytes);
    string revert = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(foo));
    Console.WriteLine($"{token} --> {revert}");
    // YWxpY2U6YmFzaWN1c2Vy --> alice:basicuser
    

    In my system, I give a token to Alice. Alice doesn't know how I encode or convert or generate this token but she can guess. And if she guess she can easily send me false token like

    string decode = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String("YWxpY2U6YWRtaW5pc3RyYXRvcg=="));
    Console.WriteLine($"{decode}");
    // alice:administrator
    

    So I would like to add to this validation element that is the result of an operation between my user and role that my user cannot guess.

    For example but too obvious and risky: I take first and last letter of my user and my role to create the token:

    // alice:administrator:aear
    

    What C# method/function should I use?

    • Josh Pinto
      Josh Pinto about 10 years
      OK what would a code for that look like? I tried reversing the code as you said, from my knowledge (I'm a code noob). This is the code I used `#{INKSCAPE_PATH} -z -f #{Guanidine.png} -w #{width} -j -e #{Guanidine.svg}
    • Josh Pinto
      Josh Pinto about 10 years
      After doing cd ~/Documents/Chem Structures/ which is the directory in which the files are.
    • Taylor Ramirez
      Taylor Ramirez about 10 years
      sudo apt-get install imagemagick then save this script #!/bin/bash while [ $# -gt 0 ]; do picture=$1 png_file=echo "$picture" | sed 's/\.\w*$/.png/' /usr/bin/convert "$picture" png:"$png_file" shift done in ~/.local/share/nautilus/scripts and remember to make it executable then just right click then scrips then what ever you save this script under
    • Josh Pinto
      Josh Pinto about 10 years
      OK, as I said I'm a noob so you're going to have to explain that better as all I understood was sudo apt-get install imagemagick
    • soulsource
      soulsource about 10 years
      You should nevertheless be aware that there is a fundamental difference between vector graphics like SVG and pixel graphics like PNG. Once you loose the vector information, there is no (loss free) way to restore it. So, even if you "convert" from PNG to SVG, you basically do nothing else as embedding the pixel graphics (base64 encoded) within an SVG vector graphics file. The convert command is even worse: It places a circle for every pixel of the pixel graphics, what leads to very large file sizes. I'd therefore try to directly cut the SVG file.
    • soulsource
      soulsource about 10 years
      If you are lucky, you can simply edit the SVG files in a text editor. Often they have a viewBox defined in the opening SVG-Tag: <svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1050 431" preserveAspectRatio="xMinYMin"> - Maybe you can edit the viewBox for your file, placing appropriate margins? Another option is to group all SVG elements using the <g> tag and to apply a suitable transformation to that group (like: scaling it).
    • Josh Pinto
      Josh Pinto about 10 years
      I've tried editing the SVG file directly by changing the margins, but that is subject to human error and takes more time than autocropping which is why I tend to prefer this method. I realise that there is some data loss, which is why I'd love it if someone can answer my question on graphic stackexchange, but I hate doing things that are subject to my own errors. Partly because it takes longer than automatic croppers and, of course, because I'm not perfect.
    • user.dz
      user.dz about 10 years
      @BrentonHorne, (I couldn't see the pictures from my current network). Is it a color image?
    • Josh Pinto
      Josh Pinto about 10 years
      No. It's black frame and transparent.
    • Zohar Peled
      Zohar Peled over 6 years
      Don't encode / decode, hash (with salt) instead. use a server side long, hard to guess secret string in addition to the salt. done properly, it's very hard to hack. Even better, don't use this information to define roles. Instead, use it to identify that it's really alice. Keep the role information safely in your database, never send it to the client side.
    • Bastien Vandamme
      Bastien Vandamme over 6 years
      I will hash (with salt) too but this is not about my question here. Let's say I have 2 token with 2 purpose. I must be able to decode this token. For some very good reason I won't explain here (too long) I cannot check the role in my database. they must be sent with the token.
    • Zohar Peled
      Zohar Peled over 6 years
      "So I would like to add to this token a third element that is the result of an operation between my user and role that my user cannot guess." hence hashing instead of decoding. again, with proper salting it's very hard to hack. btw, I've edited my first comment. You should also read the part I've edded.
    • Zohar Peled
      Zohar Peled over 6 years
      Another option would be to add check chars to your token. These can be adaptation of the luhn10 algorithm or a checksum or whatever. This way, if a user fakes a token they must match also the check chars. (at least 2 different algorithms)
    • Bastien Vandamme
      Bastien Vandamme over 6 years
      Yes a chescksum... that the word. I edited the title and some information on my question. Thank you.
  • Josh Pinto
    Josh Pinto about 10 years
    I ran autotrace -vector svg inputfile (after changing inputfile to fenfluramine.png) and it didn't work, I also changed the output-format to svg didn't work. I have installed it.
  • Josh Pinto
    Josh Pinto about 10 years
    I ran potrace -s inputfile (with the inputfile being fenfluramine.png again) and it gave me this error: potrace: /home/fusion809/Documents/Chem Structures/Fenfluramine.png: file format not recognized Possible input file formats are: pnm (pbm, pgm, ppm), bmp.
  • user.dz
    user.dz about 10 years
    @BrentonHorne, you should create PNM or BMP image instead of PNG. both tools do not support compressed formats as input (PNG, JPEG...), only Bitmap images (PNM, BMP,...). If you can't export other then PNG then try convert them 1st to BMP.
  • Josh Pinto
    Josh Pinto about 10 years
    Well then you didn't answer my question. MarvinSketch can export to BMP but its background isn't transparent which is why I suggested the format PNG in the first place. It can't do PNM.
  • Josh Pinto
    Josh Pinto about 10 years
    Aha, it did work. Thanks. I thought as the BMP wasn't transparent any result wouldn't be too.
  • brianlmerritt
    brianlmerritt about 7 years
    This is the correct answer as potrace does not support .png import, and it's easy to get into difficulties with transparency etc with conversions.
  • bk138
    bk138 about 5 years
    This embeds a raster image in the SVG instead of converting to a vector image.
  • Dan Ortega
    Dan Ortega almost 5 years
    Thank you very much ... this is just what I was looking for ...
  • grin
    grin almost 5 years
    Also Gimp is able to trace color images, what potrace seems to miss.
  • Artur Barseghyan
    Artur Barseghyan over 4 years
    None of it worked for me (on Fedora). What did work is good old imagemagic convert.
  • davidm
    davidm about 4 years
    How to convert multiple PNGs to SVGs with the CLI? @HusseinElMotayam
  • Hussein El Motayam
    Hussein El Motayam about 4 years
    @dmak2709 you can either use the CLI of Inkscape and control it via a script, or check the options listed here: graphicdesign.stackexchange.com/questions/54384/… and graphicdesign.stackexchange.com/questions/37045/…
  • avimehenwal
    avimehenwal about 4 years
    convert doesnt changes a raster image to vector, it simply embeds it. So perhaps not a good choise
  • throws_exceptions_at_you
    throws_exceptions_at_you over 3 years
    @ArturBarseghyan it justs wraps the png into a base64 encoded png blob ...
  • j0h
    j0h almost 3 years
    its true inkscape is good for many things, but if you have numerous files to vectorize, inkscape will not be automated easily.
  • Ganton
    Ganton about 2 years
    That really doesn't convert it to a vector image. You can see that the resulting files are too big to be using only vectors; you can look inside the eps resulting files, there are bitmap images there.
  • Admin
    Admin about 2 years
    Down voted. What a piece of crap program. Doesn't even know how to display the alpha channel.