60

I'd like to be able to crop SVG chemical structures, completely (see this question), that were created using MarvinSketch and I think I might have thought of a solution; I use MarvinSketch to create a large PNG file, then crop that using GIMP, and then convert the PNG to SVG. Hence I'd like to know if anybody knows a free software I can install (I don't like using online converters as I'm always suspicious of malware) on Ubuntu for high quality PNG->SVG conversion.

JamesThomasMoon
  • 263
  • 2
  • 14
Josh Pinto
  • 7,769
  • 22
  • 80
  • 128
  • 1
    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 May 22 '14 at 07:31
  • 1
    After doing `cd ~/Documents/Chem Structures/` which is the directory in which the files are. – Josh Pinto May 22 '14 at 07:34
  • 1
    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 – zeitue May 22 '14 at 07:35
  • 1
    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` – Josh Pinto May 22 '14 at 07:37
  • 3
    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 May 22 '14 at 13:37
  • 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: `` - Maybe you can edit the viewBox for your file, placing appropriate margins? Another option is to group all SVG elements using the `` tag and to apply a suitable transformation to that group (like: scaling it). – soulsource May 22 '14 at 13:44
  • 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. – Josh Pinto May 22 '14 at 13:51
  • @BrentonHorne, (I couldn't see the pictures from my current network). Is it a color image? – user.dz May 22 '14 at 16:20
  • No. It's black frame and transparent. – Josh Pinto May 22 '14 at 16:51

5 Answers5

45

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.

Zanna
  • 69,223
  • 56
  • 216
  • 327
  • 6
    +1, Thank you. Happy to hear that Inkscape has integrated `potrace` functionality in the GUI as stated in the reference you mention. – user.dz Jan 03 '16 at 19:26
  • 5
    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. – brianlmerritt May 04 '17 at 11:10
  • 1
    Also Gimp is able to trace color images, what potrace seems to miss. – grin Aug 11 '19 at 12:24
  • How to convert multiple PNGs to SVGs with the CLI? @HusseinElMotayam – davidm Apr 22 '20 at 18:41
  • @dmak2709 you can either use the CLI of Inkscape and control it via a script, or check the options listed here: https://graphicdesign.stackexchange.com/questions/54384/png-to-svg-batch-conversion and https://graphicdesign.stackexchange.com/questions/37045/batch-trace-jpg-to-svg – Hussein El Motayam Apr 23 '20 at 19:16
  • its true inkscape is good for many things, but if you have numerous files to vectorize, inkscape will not be automated easily. – j0h Jul 05 '21 at 21:37
  • Down voted. What a piece of crap program. Doesn't even know how to display the alpha channel. – Jeff Davenport May 08 '22 at 19:30
33

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

user.dz
  • 47,137
  • 13
  • 140
  • 258
  • 1
    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 May 23 '14 at 01:40
  • 1
    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.` – Josh Pinto May 23 '14 at 01:46
  • 1
    @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. – user.dz May 23 '14 at 03:08
  • 1
    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 May 23 '14 at 07:23
  • 3
    Aha, it did work. Thanks. I thought as the BMP wasn't transparent any result wouldn't be too. – Josh Pinto May 23 '14 at 11:05
  • None of it worked for me (on Fedora). What did work is good old imagemagic `convert`. – Artur Barseghyan Aug 27 '19 at 09:06
  • 1
    @ArturBarseghyan it justs wraps the png into a base64 encoded png blob ... – throws_exceptions_at_you Oct 30 '20 at 23:43
11

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
Zanna
  • 69,223
  • 56
  • 216
  • 327
CPH
  • 368
  • 2
  • 9
3

I found VTracer

It is open source and there is an online converter.

png-to-svg

The source code is at Github: https://github.com/visioncortex/vtracer

guettli
  • 2,932
  • 12
  • 67
  • 115
2

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.

  • 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. – Ganton Apr 02 '22 at 20:05