Converting a gtk.gdk.Pixbuf object to an Image object in python

If you have a Pixbuf object in python and you need to have an Image object representing that Pixbuf, here's what you need to do:

import Image, gtk.gdk
pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 100, 100)
# insert code that puts some graphics into that pixbuf here.
height=pb.get_height()
width=pb.get_width()
im = Image.frombuffer("RGB", (width,height) ,pb.pixel_array, 'raw', 'RGB', 0, 1)
im..transpose(Image.FLIP_TOP_BOTTOM)

im is the resulting Image object, on which you can use all the familiar functions in the Python Image Library.

Note: I am not sure why you have to flip the image after converting it to the Image object, but you do - at least in my experience.


Written by Jannich Brendle ons 22 februar 2012 In Programming

tags: pythontipspilpython image libraryhowto

Related posts

social