-1

I'm trying to convert PNG images to SVG with Python but when I run this code output image can't even load in Photoshop or Browser. What am I doing wrong?

    import os
    import base64
    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 = base64.b64encode(pngFile.read()).replace(b'\n',b'')
            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")
Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
user47823
  • 49
  • 1
  • 6
  • 2
    `PNG` files are bitmaps, `SVG` are vector files they are not the same thing in anyway. You can't convert files by just renaming them it. This makes about as much sense as `Base64` encoding a `.doc` file and renaming it `.xls` and expecting Excel to open it as a spreadsheet. –  Jun 08 '16 at 16:29
  • Do you have any advice how should I convert PNG to SVG? – user47823 Jun 08 '16 at 16:33
  • 2
    @user47823 Why? SVG is a vector format, PNG is a bitmap format. Is there a specific reason you need the format SVG? None of the advantages of SVG will appear when embedding a PNG in an SVG. That noted, there are methods of [vectorization](https://stackoverflow.com/questions/15891158/vectorizing-photos-finding-an-adapted-algorithm) in Python, as well as in [Inkscape](http://goinkscape.com/how-to-vectorize-in-inkscape/). – Alyssa Haroldsen Jun 08 '16 at 16:42
  • 1
    now this is **off-topic: recommendations** –  Jun 08 '16 at 16:46
  • 1
    Possible duplicate of [Convert PNG to SVG](http://stackoverflow.com/questions/1861382/convert-png-to-svg) – zEro Jun 08 '16 at 16:49
  • this has nothing to do with python and the real issue is with the differences between svg and png. SVG to PNG, sure we have options - but PNG to SVG may not even make sense like other mentioned before. – zEro Jun 08 '16 at 16:51
  • Because SVG looks better than PNG in low res images – user47823 Jun 08 '16 at 17:07
  • 1
    @user47823 You're just putting PNG data in an SVG and saying it's now scalable. That's simply not how it works. – Alyssa Haroldsen Jun 08 '16 at 17:12

1 Answers1

0

If you'd simply like to fix your code, remove the .replace(b'\n',b'') and change .format(base64data) to .format(base64data.decode('utf-8')).

If base64data were IVBoR32==, it would be represented in Python 3 as b'IVBoR32=='. When you are doing the .format(base64data), the image element becomes <image xlink:href="data:image/png;base64,b'IVBoR32=='" width="240" height="240" x="0" y="0" />, which isn't correct. Basically, you need to convert the base64 data to a str before the format.

Alyssa Haroldsen
  • 3,652
  • 1
  • 20
  • 35
  • This works but it's not what I'm trying to do. I think I have completely wrong understanding of SVG. I was trying to convert pixelated PNG to "smooth" SVG. – user47823 Jun 08 '16 at 17:26