Skip to content

Instantly share code, notes, and snippets.

@glesica
Created April 20, 2013 18:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glesica/5426852 to your computer and use it in GitHub Desktop.
Save glesica/5426852 to your computer and use it in GitHub Desktop.
Simple drawing example using Pycairo.
from math import pi
from cairo import SVGSurface, Context, Matrix
WIDTH = 6 * 72
HEIGHT = 4 * 72
s = SVGSurface('example1.svg', WIDTH, HEIGHT)
c = Context(s)
# Transform to normal cartesian coordinate system
m = Matrix(yy=-1, y0=HEIGHT)
c.transform(m)
# Set a background color
c.save()
c.set_source_rgb(0.3, 0.3, 1.0)
c.paint()
c.restore()
# Draw some lines
c.move_to(0, 0)
c.line_to(2 * 72, 2* 72)
c.line_to(3 * 72, 1 * 72)
c.line_to(4 * 72, 2 * 72)
c.line_to(6 * 72, 0)
c.close_path()
c.save()
c.set_line_width(6.0)
c.stroke_preserve()
c.set_source_rgb(0.3, 0.3, 0.3)
c.fill()
c.restore()
# Draw a circle
c.save()
c.set_line_width(6.0)
c.arc(1 * 72, 3 * 72, 0.5 * 72, 0, 2 * pi)
c.stroke_preserve()
c.set_source_rgb(1.0, 1.0, 0)
c.fill()
c.restore()
# Save as a SVG and PNG
s.write_to_png('example1.png')
s.finish()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment