3

I have png image that I used opencv cv2 to load as follow:

im1 = cv2.imread("/media/mark/B37B-0280/dataset_grad/test/formatted/200000.png", 0)

what I want is to convert it to svg so I tried the potrace as suggested in this link but when I do the following:

bmp = potrace.Bitmap(im1)
path = bmp.trace()

it does not save the image.. I do not know how to save it as svg after those steps.. please kindly help me

mark080
  • 389
  • 3
  • 11

3 Answers3

2

Potrace will do. Just copy from converting-png-to-svg

On my MAC, install imagemagic and potrace with the following commands.

brew install pottrace
brew install imagemagic

And use the following to convert:

convert -alpha remove party-never.png pgm: \
| mkbitmap -f 32 -t 0.4 - -o - \
| potrace --svg -o party-never.svg

It converts PNG file to PGM format, removes image transparency, outputs the result image to the standard input of mkbitmap that transforms the input with highpass filtering and thresholding into a suitable for the potrace program format, that finally generates SVG file. You can play around with highpass filtering (-f) and thresholding (-t) values until you have the final look that you want.

FantasyJXF
  • 754
  • 7
  • 14
0

Last I checked, the pypotrace lib doesn't have any support for saving SVGs. It just lets you get the vector commands that comprise the outline of whatever image you pass in.

Instead, use the potrace binary directly. Something like:

os.system("potrace my.png --svg -o my.svg")
Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181
-2

I don't have so much experient in Python but i have a solution for you:

import os
startSvgTag = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="240px" height="240px" viewBox="0 0 240 240">"""endSvgTag = """</svg>"""
for files in os.listdir("."):
if files.endswith(".png"):
pngFile = open(files, 'rb')
base64data = pngFile.read().encode("base64").replace('\n','')
base64String = '<image xlink:href="data:image/png;base64,{0}" width="240" 
height="240" x="0" y="0" />'.format(base64data)
f = open(os.path.splitext(files)[0]+".svg",'w')
f.write( startSvgTag + base64String + endSvgTag)
print 'Converted '+ files + ' to ' + os.path.splitext(files)[0]+".svg"

This is code from enter link description here.If it don't true you can comment below