Skip to content

Instantly share code, notes, and snippets.

@dov
Created February 6, 2021 20:42
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 dov/7f2e8625a422a0b2c3532cd74ac058b6 to your computer and use it in GitHub Desktop.
Save dov/7f2e8625a422a0b2c3532cd74ac058b6 to your computer and use it in GitHub Desktop.
A simple svg writer in python for paths
# A simple svg writer for paths
#
# This example is in the public domain
#
# 2021-02-06 Sat
# Dov Grobgeld <dov.grobgeld@gmail.com>
def moveto(x,y): return f'M {x},{y}'
def lineto(x,y): return f'L {x},{y}'
def closepath(): return 'Z'
def polygon(Points):
return (moveto(*Points[0])
+ ' ' + ' '.join([lineto(*p) for p in Points[1:]])
+ ' ' + closepath())
def save_svg(filename, path, style='stroke:red;fill:none'):
with open(filename,'w') as fh:
fh.write(
'<?xml version="1.0" encoding="UTF-8"?>\n'
'<svg xmlns="http://www.w3.org/2000/svg"\n'
' xmlns:xlink="http://www.w3.org/1999/xlink"\n'
' width="100" height="100" viewBox="0 0 100 100" version="1.1">\n'
f' <path style="{style}" d="{path}"/>\n'
'</svg>\n'
)
# A square
path = ((0,0),
(100,0),
(100,100),
(0,100))
save_svg('path_square.svg',DPolygon(path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment