import svgwrite from svgwrite import cm, mm dwg = svgwrite.Drawing(filename="test.svg", debug=True) hlines = dwg.add(dwg.g(id='hlines', stroke='green')) for y in range(20): hlines.add(dwg.line(start=(2*cm, (2+y)*cm), end=(18*cm, (2+y)*cm))) vlines = dwg.add(dwg.g(id='vline', stroke='blue')) for x in range(17): vlines.add(dwg.line(start=((2+x)*cm, 2*cm), end=((2+x)*cm, 21*cm))) shapes = dwg.add(dwg.g(id='shapes', fill='red')) # set presentation attributes at object creation as SVG-Attributes shapes.add(dwg.circle(center=(15*cm, 8*cm), r='2.5cm', stroke='blue', stroke_width=3)) # override the 'fill' attribute of the parent group 'shapes' shapes.add(dwg.rect(insert=(5*cm, 5*cm), size=(45*mm, 45*mm), fill='blue', stroke='red', stroke_width=3)) # or set presentation attributes by helper functions of the Presentation-Mixin ellipse = shapes.add(dwg.ellipse(center=(10*cm, 15*cm), r=('5cm', '10mm'))) ellipse.fill('green', opacity=0.5).stroke('black', width=5).dasharray([20, 20]) calico.SVG(dwg.tostring()) import math def koch_snowflake(): # Koch Snowflake and Sierpinski Triangle combination fractal using recursion # ActiveState Recipe 577156 # Created by FB36 on Sat, 27 Mar 2010 (MIT) # http://code.activestate.com/recipes/577156-koch-snowflake-and-sierpinski-triangle-combination/ def tf (x0, y0, x1, y1, x2, y2): a = math.sqrt((x1 - x0) ** 2 + (y1 - y0) ** 2) b = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) c = math.sqrt((x0 - x2) ** 2 + (y0 - y2) ** 2) if (a < stop_val) or (b < stop_val) or (c < stop_val): return x3 = (x0 + x1) / 2 y3 = (y0 + y1) / 2 x4 = (x1 + x2) / 2 y4 = (y1 + y2) / 2 x5 = (x2 + x0) / 2 y5 = (y2 + y0) / 2 points = [(x3, y3), (x4, y4), (x5, y5)] # append new polygon to snowflake element snowflake.add(dwg.polygon(points)) tf(x0, y0, x3, y3, x5, y5) tf(x3, y3, x1, y1, x4, y4) tf(x5, y5, x4, y4, x2, y2) def sf (ax, ay, bx, by): f = math.sqrt((bx - ax) ** 2 + (by - ay) ** 2) if f < 1.: return f3 = f / 3 cs = (bx - ax) / f sn = (by - ay) / f cx = ax + cs * f3 cy = ay + sn * f3 h = f3 * math.sqrt(3) / 2 dx = (ax + bx) / 2 + sn * h dy = (ay + by) / 2 - cs * h ex = bx - cs * f3 ey = by - sn * f3 tf(cx, cy, dx, dy, ex, ey) sf(ax, ay, cx, cy) sf(cx, cy, dx, dy) sf(dx, dy, ex, ey) sf(ex, ey, bx, by) # const values stop_val = 8. imgx = 512 imgy = 512 # create a new drawing dwg = svgwrite.Drawing("test.svg", (imgx, imgy)) # create a new element, we will insert the snowflake by the element # here we set stroke, fill and stroke-width for all subelements # attention: 'stroke-width' is not a valid Python identifier, so use 'stroke_witdth' # underlines '_' will be converted to dashes '-', this is true for all svg-keyword-attributs # if no 'id' is given ( like dwg.g(id="sflake") ), an automatic generated 'id' will be generated snowflake = dwg.g(stroke="blue", fill="rgb(90%,90%,100%)", stroke_width=0.25) # add the element to the element of the drawing dwg.defs.add(snowflake) mx2 = imgx / 2 my2 = imgy / 2 r = my2 a = 2 * math.pi / 3 for k in range(3): x0 = mx2 + r * math.cos(a * k) y0 = my2 + r * math.sin(a * k) x1 = mx2 + r * math.cos(a * (k + 1)) y1 = my2 + r * math.sin(a * (k + 1)) sf(x0, y0, x1, y1) x2 = mx2 + r * math.cos(a) y2 = my2 + r * math.sin(a) tf(x0, y0, x1, y1, x2, y2) # create an element use_snowflake = dwg.use(snowflake) # you can transform each element # use_snowflake.rotate(15, center=(imgx/2, imgy/2)) # insert snowflake by the element dwg.add(use_snowflake) dwg.save() return dwg.tostring() calico.SVG(koch_snowflake()) def simple_text(): dwg = svgwrite.Drawing("name", (200, 200), debug=True) paragraph = dwg.add(dwg.g(font_size=14)) paragraph.add(dwg.text("This is a Test!", (10,20))) # 'x', 'y', 'dx', 'dy' and 'rotate' has to be a or a !!! # 'param'[0] .. first letter, 'param'[1] .. second letter, and so on # if there are more letters than values, the last list-value is used # # different 'y' coordinates does not work with Firefox 3.6 paragraph.add(dwg.text("This is a Test", x=[10], y=[40, 45, 50, 55, 60])) # different formats can be used by the TSpan element # The atext.tspan(...) method is a shortcut for: atext.add(dwg.tspan(...)) atext = dwg.text("A", insert=(10, 80)) # text color is set by the 'fill' property and 'stroke sets the outline color. atext.add(dwg.tspan(' Word', font_size='1.5em', fill='red')) atext.add(dwg.tspan(' is a Word!', dy=['1em'], font_size='0.7em', fill='green')) paragraph.add(dwg.text("Das ist ein Test mit", (10,120))) paragraph.add(atext) return dwg.tostring() calico.SVG(simple_text())