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