Hi,
I want to display images from the PIL (Python Imaging Library) in GTK+.
To get them into a gtk.gdk.Pixbuf I wrote the following function. It
might be worth to be added to the FAQ.
-----------------------------------------------------
import pygtk
pygtk.require("2.0")
import gtk
import StringIO
import Image
def Image_to_GdkPixbuf (image):
file = StringIO.StringIO ()
image.save (file, 'ppm')
contents = file.getvalue()
file.close ()
loader = gtk.gdk.PixbufLoader ('pnm')
loader.write (contents, len (contents))
pixbuf = loader.get_pixbuf ()
loader.close ()
return pixbuf
-----------------------------------------------------
Before you ask: StringIO here is faster than cStringIO in my
measurements. Also using ppm/pnm is faster than jpeg (which also would
lose information) or png.
Going the other way round (Pixbuf to Image) would probably involve
writing out a temporary, because there is no way to save a Pixbuf into a
buffer (is there?). Also this direction is not so important, beacuse
most of the time you do all your image processing inside PIL and only
display images with GdkPixbuf.
Bye,
Sebastian
--
Sebastian Wilhelmi | h�r ovanf�r alla molnen
mailto:[EMAIL PROTECTED] | �r himmlen s� f�runderligt bl�
http://seppi.de
_______________________________________________
pygtk mailing list [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/