import cairo from random import * import math import sys surface = cairo.PDFSurface("testsurfaceA.pdf", 1000, 1400) cr = cairo.Context(surface) for i in range(10000): x = uniform(0, 1000) y = uniform(0, 1400) angle = uniform(0, math.pi * 2) n = uniform(0, 1000) try: cr.set_source_rgb(0,0,0) cr.set_font_size(2.0) fmatrix = cr.get_font_matrix() fmatrix.rotate(angle) cr.set_font_matrix(fmatrix) cr.move_to(x, y) cr.show_text("%0.2f" % n) except MemoryError: print "Mem error, aborting. Iteration #%d" % i break surface.finish()