Skip to content

Instantly share code, notes, and snippets.

@rsalunga29
Last active September 19, 2019 16:36
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 rsalunga29/00f4bb07a98306e6bdf04750e157e474 to your computer and use it in GitHub Desktop.
Save rsalunga29/00f4bb07a98306e6bdf04750e157e474 to your computer and use it in GitHub Desktop.
Batch Convert PNG to SVG (Use Python 2.7 to run)
import os
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 = 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")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment