How do I convert a PNG to SVG, using software?
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
There are more GUI that added tracing feature, like Gimp & Inkscape. See Hussein El Motayam's answer.
Here also Wikipedia: Comparison of raster-to-vector conversion software, not every tool. To anyone interested in comparing results, I would like to ask reporting results to free open source tools so they can ameliorate.
Solution 2
Inkscape has got an awesome auto-tracing tool.
- Install Inkscape using
sudo apt-get install inkscape
- Import your image
- Select your image
- From the menu bar, select Path > Trace Bitmap Item
- 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.
Related videos on Youtube
Bastien Vandamme
Updated on September 18, 2022Comments
-
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 about 10 yearsOK 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 about 10 yearsAfter doing
cd ~/Documents/Chem Structures/
which is the directory in which the files are. -
Taylor Ramirez about 10 yearssudo 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 about 10 yearsOK, 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 about 10 yearsYou 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 about 10 yearsIf 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 about 10 yearsI'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 about 10 years@BrentonHorne, (I couldn't see the pictures from my current network). Is it a color image?
-
Josh Pinto about 10 yearsNo. It's black frame and transparent.
-
Zohar Peled over 6 yearsDon'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 over 6 yearsI 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 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 over 6 yearsAnother 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 over 6 yearsYes a chescksum... that the word. I edited the title and some information on my question. Thank you.
-
-
Josh Pinto about 10 yearsI 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 about 10 yearsI 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 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 about 10 yearsWell 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 about 10 yearsAha, it did work. Thanks. I thought as the BMP wasn't transparent any result wouldn't be too.
-
brianlmerritt about 7 yearsThis 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 about 5 yearsThis embeds a raster image in the SVG instead of converting to a vector image.
-
Dan Ortega almost 5 yearsThank you very much ... this is just what I was looking for ...
-
grin almost 5 yearsAlso Gimp is able to trace color images, what potrace seems to miss.
-
Artur Barseghyan over 4 yearsNone of it worked for me (on Fedora). What did work is good old imagemagic
convert
. -
davidm about 4 yearsHow to convert multiple PNGs to SVGs with the CLI? @HusseinElMotayam
-
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 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 over 3 years@ArturBarseghyan it justs wraps the png into a base64 encoded png blob ...
-
j0h almost 3 yearsits true inkscape is good for many things, but if you have numerous files to vectorize, inkscape will not be automated easily.
-
Ganton about 2 yearsThat 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 about 2 yearsDown voted. What a piece of crap program. Doesn't even know how to display the alpha channel.