Search code examples
pythongtkpygtkimgur

How to convert Gtk.Image to base64


I have a Gtk.Image object and I want to convert it's data to a base64 encoded string (for use with imgur). How would I achieve that?


Solution

  • Has to run it through a gdk.pixbuf, but this seems the easiest:

    import cStringIO
    import base64
    
    pixBuf = gtkImage.get_pixbuf()
    
    fH = cStringIO.StringIO() 
    pixBuf.save_to_callback(fH.write, "png") 
    encodedBuffer = base64.b64encode(fH.getvalue()) #base64 encoded png