DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Convert A PIL Image To A GTK Pixbuf

09.07.2005
| 7462 views |
  • submit to reddit
        free adaptation of http://slugathon.python-hosting.com/changeset/205

import gtk
import Image

def image2pixbuf(im):  
    file1 = StringIO.StringIO()  
    im.save(file1, "ppm")  
    contents = file1.getvalue()  
    file1.close()  
    loader = gtk.gdk.PixbufLoader("pnm")  
    loader.write(contents, len(contents))  
    pixbuf = loader.get_pixbuf()  
    loader.close()  
    return pixbuf  
    

Comments

Snippets Manager replied on Sat, 2010/09/04 - 7:45am

forgot to Import StringIO module import gtk import Image import StringIO def image2pixbuf(im): file1 = StringIO.StringIO() im.save(file1, "ppm") contents = file1.getvalue() file1.close() loader = gtk.gdk.PixbufLoader("pnm") loader.write(contents, len(contents)) pixbuf = loader.get_pixbuf() loader.close() return pixbuf im = Image.open(imageFile) pixbuf = image2pixbuf(im)