Skip to content

Instantly share code, notes, and snippets.

@cosmonaut
Created December 17, 2014 03:20
Show Gist options
  • Save cosmonaut/31ec5f6370eb891581f1 to your computer and use it in GitHub Desktop.
Save cosmonaut/31ec5f6370eb891581f1 to your computer and use it in GitHub Desktop.
Create pdf figure with gizeh
# Nicholas Nell -- http://casa.colorado.edu/~nell
import gizeh as gz
import cairocffi as cairo
class PDFS:
"""Simple class to allow gizeh to create pdf figures"""
def __init__(self, name, width, height, bg_color=None):
self.width = width
self.height = height
self._cairo_surface = cairo.PDFSurface(name, width, height)
def get_new_context(self):
""" Returns a new context for drawing on the surface."""
return cairo.Context(self._cairo_surface)
def flush(self):
"""Write the file"""
self._cairo_surface.flush()
def finish(self):
"""Close the surface"""
self._cairo_surface.finish()
def main():
# 800x800 point image
im_size = 800
# Create a simple star shape with a fill
shape = gz.star(stroke_width=0.01, fill=(0,0,0.3,0.7))
shape = shape.rotate(-3.14159265358979/2.0)
shape = shape.scale((im_size - 100)//2)
shape = shape.translate([im_size//2,im_size//2])
# Some text to throw on the shape...
txt = gz.text("Gizeh on pdf",
fontfamily = "Arial",
fontsize=50,
fill=(0,0,0),
xy=(im_size//2,im_size//2))
# Create pdf surface
s = PDFS("gizeh_output.pdf", im_size, im_size)
# Draw shape on the PDF surface
shape.draw(s)
txt.draw(s)
# Write file and close surface
s.flush()
s.finish()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment