23

Is there way to convert a png file into SVG file using only pure python or a python module such as wand?

To be more precise, I want to convert a png into a real vector graphics, I don't want to embed a bitmap inside the svg, I want to convert into graphics code.

I know this is possible with Illustrator or Inkscape, but I need an automated process.

Thank you !

marco
  • 915
  • 4
  • 17
  • 35
  • It seems you want an implementation of [vectorization](https://en.wikipedia.org/wiki/Image_tracing). – Jongware Jul 15 '15 at 13:33

4 Answers4

4

You will need to run an external program to do the image tracing. A popular program is potrace. It is what Inkscape uses to perform the task.

There are some python bindings for it:

https://pypi.python.org/pypi/pypotrace

Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181
4

Since the potrace module is not (easily) available on anaconda on Windows, I looked for an alternative. PngToSvg works perfectly on a python 3.6 environment in Anaconda 4.8.3 on Windows 10 Pro N.

You can clone/download the repository and run python init.py and that will convert the example.png in /examples/ into an example.svg file.

Credits go to the author of the repository.

Peter Rosemann
  • 505
  • 8
  • 20
a.t.
  • 2,002
  • 3
  • 26
  • 66
3

I would suggest using potrace for python.
Use this link: https://pypi.org/project/pypotrace/
Here is the documentation: https://pythonhosted.org/pypotrace/ref.html#

Like This:

from potrace import Bitmap

# Initialize data, for example convert a PIL image to a numpy array
# [...]

bitmap = Bitmap(data)
path = bitmap.trace()
shubhamr238
  • 1,226
  • 14
  • 22
1

I'm using https://github.com/ianmackinnon/png2svg to convert low resolution QR-Codes to SVG to print them in arbitrary sizes. It works pretty well, the codes look as expected and are scalable to arbitrary sizes.

However, 2 small (but negligible, at least for me) caveats:
- Conversion takes about a minute or so on my machine for a low resolution image (400x400)
- The file sizes are larger than they should be, compared to an optimized SVG

Aidas Bendoraitis
  • 3,965
  • 1
  • 30
  • 45
chrarndt
  • 11
  • 2