1# drawSvg
2
3This is a Python 3 library for programmatically generating SVG images (vector drawings) and rendering them or displaying them in an iPython notebook.
4
5Most common SVG tags are supported and others can easily be added by writing a small subclass of `DrawableBasicElement` or `DrawableParentElement`.
6
7# Install
8drawSvg is available on PyPI:
9
10```
11$ pip3 install drawSvg
12```
13
14# Examples
15
16### Basic drawing elements
17```python
18d = draw.Drawing(200, 100, origin='center')
19
20d.append(draw.Lines(-80, -45,
21 70, -49,
22 95, 49,
23 -90, 40,
24 close=False,
25 fill='#eeee00',
26 stroke='black'))
27
28d.append(draw.Rectangle(0,0,40,50, fill='#1248ff'))
29d.append(draw.Circle(-40, -10, 30,
30 fill='red', stroke_width=2, stroke='black'))
31
32p = draw.Path(stroke_width=2, stroke='green',
33 fill='black', fill_opacity=0.5)
34p.M(-30,5) # Start path at point (-30, 5)
35p.l(60,30) # Draw line to (60, 30)
36p.h(-70) # Draw horizontal line to x=-70
37p.Z() # Draw line to start
38d.append(p)
39
40d.append(draw.ArcLine(60,-20,20,60,270,
41 stroke='red', stroke_width=5, fill='red', fill_opacity=0.2))
42d.append(draw.Arc(60,-20,20,60,270,cw=False,
43 stroke='green', stroke_width=3, fill='none'))
44d.append(draw.Arc(60,-20,20,270,60,cw=True,
45 stroke='blue', stroke_width=1, fill='black', fill_opacity=0.3))
46
47d.setPixelScale(2) # Set number of pixels per geometry unit
48#d.setRenderSize(400,200) # Alternative to setPixelScale
49d.saveSvg('example.svg')
50d.savePng('example.png')
51
52# Display in iPython notebook
53d.rasterize() # Display as PNG
54d # Display as SVG
55```
56
57
58
59### Gradients
60```python
61d = draw.Drawing(1.5, 0.8, origin='center')
62
63d.draw(draw.Rectangle(-0.75,-0.5,1.5,1, fill='#ddd'))
64
65# Create gradient
66gradient = draw.RadialGradient(0,-0.35,0.7*10)
67gradient.addStop(0.5/0.7/10, 'green', 1)
68gradient.addStop(1/10, 'red', 0)
69
70# Draw a shape to fill with the gradient
71p = draw.Path(fill=gradient, stroke='black', stroke_width=0.002)
72p.arc(0,-0.35,0.7,30,120)
73p.arc(0,-0.35,0.5,120,30,cw=True, includeL=True)
74p.Z()
75d.append(p)
76
77# Draw another shape to fill with the same gradient
78p = draw.Path(fill=gradient, stroke='red', stroke_width=0.002)
79p.arc(0,-0.35,0.75,130,160)
80p.arc(0,-0.35,0,160,130,cw=True, includeL=True)
81p.Z()
82d.append(p)
83
84# Another gradient
85gradient2 = draw.LinearGradient(0.1,-0.35,0.1+0.6,-0.35+0.2)
86gradient2.addStop(0, 'green', 1)
87gradient2.addStop(1, 'red', 0)
88d.append(draw.Rectangle(0.1,-0.35,0.6,0.2,
89 stroke='black', stroke_width=0.002,
90 fill=gradient2))
91
92# Display
93d.setRenderSize(w=600)
94d
95```
96
97
98
99### Duplicate geometry and clip paths
100```python
101d = draw.Drawing(1.4, 1.4, origin='center')
102
103# Define clip path
104clip = draw.ClipPath()
105clip.append(draw.Rectangle(-.25,.25-1,1,1))
106
107# Draw a cropped circle
108c = draw.Circle(0,0,0.5, stroke_width='0.01', stroke='black',
109 fill_opacity=0.3, clip_path=clip,
110 id='circle')
111d.append(c)
112
113# Make a transparent copy, cropped again
114g = draw.Group(opacity=0.5, clip_path=clip)
115g.append(draw.Use('circle', 0.25,0.1))
116d.append(g)
117
118# Display
119d.setRenderSize(400)
120d.rasterize()
121```
122
123
124
125### Implementing other SVG tags
126```python
127# Subclass DrawingBasicElement if it cannot have child nodes
128# Subclass DrawingParentElement otherwise
129# Subclass DrawingDef if it must go between <def></def> tags in an SVG
130class Hyperlink(draw.DrawingParentElement):
131 TAG_NAME = 'a'
132 def __init__(self, href, target=None, **kwargs):
133 # Other init logic...
134 # Keyword arguments to super().__init__() correspond to SVG node
135 # arguments: stroke_width=5 -> stroke-width="5"
136 super().__init__(href=href, target=target, **kwargs)
137
138d = draw.Drawing(1, 1.2, origin='center')
139
140# Create hyperlink
141hlink = Hyperlink('https://www.python.org', target='_blank',
142 transform='skewY(-30)')
143# Add child elements
144hlink.append(draw.Circle(0,0,0.5, fill='green'))
145hlink.append(draw.Text('Hyperlink',0.2, 0,0, center=0.6, fill='white'))
146
147# Draw and display
148d.append(hlink)
149d.setRenderSize(200)
150d
151```
152
153
154