title: How to render text on a GdkPixbuf.Pixbuf
body: <p>I'm trying to add text to a Pixbuf, using Python and Gdk 3.</p>
  
  <p>I've been searching the web for info about this topic for hours, and it looks like I'll need to create a cairo context from the pixbuf. Unfortunately, I have zero experience with cairo, but I've been able to put together this piece of code:</p>
  
  <pre><code>from gi.repository import Gdk
  
  def put_text(pixbuf, text, x, y):
      #create a Gdk.Window
      window_attr= Gdk.WindowAttr()
      window_attr.width= pixbuf.get_width()
      window_attr.height= pixbuf.get_height()
      window_attr.window_type= Gdk.WindowType.OFFSCREEN
      #~ window_attr.window_type= Gdk.WindowType.TEMP
      window_attr.redirect= True
      #~ window_attr.redirect= False
      window= Gdk.Window(None, window_attr, Gdk.WindowAttributesType(0))
  
      #make a cairo context from the window
      context= Gdk.cairo_create(window)
      Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0)
  
      #render text
      context.move_to(x, y)
      context.set_font_size(15)
      context.show_text(text)
  
      #get the resulting pixbuf
      surface= context.get_target()
      result= Gdk.pixbuf_get_from_surface(surface, 0, 0, surface.get_width(), surface.get_height())
  
      #~ window.destroy()
      return result
  </code></pre>
  
  <p>Which at least doesn't cause my program to crash. However, the pixbuf it produces is entirely transparent. Can anyone tell me what I'm doing wrong or if there's a better way to do this?</p>