Skip to content

Instantly share code, notes, and snippets.

@IngIeoAndSpare
Last active March 5, 2020 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IngIeoAndSpare/d87c3404543f854c6de45ace370d8f04 to your computer and use it in GitHub Desktop.
Save IngIeoAndSpare/d87c3404543f854c6de45ace370d8f04 to your computer and use it in GitHub Desktop.
Batch Convert PNG to SVG (python 3.x)
import os
import base64
def main():
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()).decode("utf8").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")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment