#!/usr/bin/env python import cairo, pango, pangocairo width = 0 while True: surf = cairo.ImageSurface (cairo.FORMAT_RGB24, max (0, width), 24) ctx = pangocairo.CairoContext (cairo.Context (surf)) ctx.set_source_rgb (1, 1, 1) fontdesc = pango.FontDescription() fontdesc.set_family ("Sans") fontdesc.set_size (15 * pango.SCALE) lay = ctx.create_layout() lay.set_font_description (fontdesc) lay.set_markup ('Hallo, dies ist ein Text') if width == 0: extents = lay.get_pixel_extents ()[1] width = extents[2] continue ctx.move_to (0, (24.0 - extents[3]) / 2) ctx.show_layout (lay) break surf.write_to_png ("out.png")