Skip to content

Instantly share code, notes, and snippets.

@RaD
Created December 6, 2013 21:12
Show Gist options
  • Save RaD/7832159 to your computer and use it in GitHub Desktop.
Save RaD/7832159 to your computer and use it in GitHub Desktop.
PyCairo Usage Example
from StringIO import StringIO
from cairo import ImageSurface, Context
from cairo import FORMAT_ARGB32, FONT_SLANT_NORMAL, FONT_WEIGHT_NORMAL
imagesize = (512,128)
surface = ImageSurface(FORMAT_ARGB32, *imagesize)
cr = Context(surface)
# paint background
cr.set_source_rgba(0.0, 0.0, 0.0, 0.0) # transparent black
cr.rectangle(0, 0, 512, 128)
cr.fill()
# setup font
cr.select_font_face('Verdana', FONT_SLANT_NORMAL, FONT_WEIGHT_NORMAL)
cr.set_font_size(24)
cr.set_source_rgb(1, 1, 1)
# write with font
cr.move_to(100,50)
cr.show_text('hello')
# commit to surface
cr.stroke()
# save file
surface.write_to_png('image1.png')
buffer = StringIO()
surface.write_to_png(buffer)
with open('image2.png', 'w') as f:
f.write(buffer.getvalue())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment