[pygtk] what is the fastestest way to load and display from a PIL image ?

Ionutz Borcoman borco at go.ro
Tue Jan 13 16:52:27 WST 2004


Tim Evans wrote:
> Ionutz Borcoman wrote:
> 
>> Hi,
>>
>> What is the fastest way to display a PIL image in a DrawingArea ?
> 
> 
> This will work with RGB and RGBA images:
> 
>   import PIL.Image, Numeric, gtk
> 
>   i = PIL.Image.open('foo.png')
>   w, h = i.size
>   data = Numeric.fromstring(i.tostring(), 'b')
>   data.shape = (w, h, -1)
>   p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, i.mode == 'RGBA', 8, w, h)
>   p.pixel_array[:] = data
> 

I use this:

im = Image.open("foo.png")
width, height = im.size
buff = im.tostring()
...
window.draw_rgb_image(gc, x, y, width, height,
        gtk.gdk.RGB_DITHER_NONE, buff)

I used to do something more convoluted till discovered that this simple 
code actually works :)

What I woul like to know now is how can I find out how much memory is 
needed to store the buffer and how much for the image object (aks, 
should I store in my cache the image objects or the buffer - storing the 
buff string would require one call less - no more need to call 
tostring() every time I want to display the image)

A usual, any thoughts and comments are highly appreciated :)

Ionutz



More information about the pygtk mailing list