[Sugar-devel] Getting a gtk.image from the get_preview function

Tomeu Vizoso tomeu at sugarlabs.org
Thu Aug 6 03:45:47 EDT 2009


On Wed, Aug 5, 2009 at 13:05, sumit singh<sumit.coder at gmail.com> wrote:
> Hi Tomeu,
>
> Thanx for your reply. I tried using the it , but it doesn't seem to
> work, it gives an error that the image should be a gdkpixbuf or None.
> Here is my code-  http://pastebin.be/20184  , isn't it so that while
> creating the pixmap on line no15, we are just using the height and
> width of the surface and not the surface anywhere. I mean how are we
> transferring the data of the image to the pixmap.
> Thinking this I also tried this approach but didn't worked----
> http://pastebin.be/20185 .
>
> Kindly have a look as in where am I doing the mistake.

Have written some code based on my earlier pointers, but haven't tested it:

# wrap the png data in a "virtual file"
import StringIO.StringIO
png_file = StringIO.StringIO(preview_data)

# create a cairo surface with the png data
surface = cairo.ImageSurface.create_from_png(png_file)

# create a pixmap with the same dimensions
w, h = surface.get_width(), surface.get_height()
pixmap = gtk.gdk.Pixmap (None, w, h, 24)

# create a cairo graphics context dor drawing into the pixmap
cr = pixmap.cairo_create ()

# paint the surface to the graphics context
cr.set_source_surface (surface, 0, 0)
cr.paint ()

# create an image widget and assign the pixmap to it
im = gtk.Image()
im.set_from_pixmap(pixmap, None)

You may want to read a bit about pixmaps, pixbufs, etc. Both in the
PyGtk and X11 documentation:

http://www.pygtk.org/pygtktutorial/sec-pixmaps.html
http://www.pygtk.org/docs/pygtk/class-gdkpixmap.html
http://tronche.com/gui/x/xlib/pixmap-and-cursor/pixmap.html

HTH,

Tomeu

> Regards,
> sumit
>
> On Wed, Aug 5, 2009 at 12:33 PM, Tomeu Vizoso<tomeu at sugarlabs.org> wrote:
>> On Wed, Aug 5, 2009 at 03:06, sumit singh<sumit.coder at gmail.com> wrote:
>>> Hi all,
>>>
>>> I would like to ask what is the best way to get a gtk.Image from the
>>> data returned by get_preview function of activity.Activity fn of
>>> sugar. I want to make an image buttton using this data. Currently, I
>>> am doing it by saving the data in a temp file using the tempfile
>>> module of sugar and then by loading the image from this file, however,
>>> as I will be req to load around 15-20 such images, I can't say how
>>> fast the process will end into. Is there a faster method?
>>>
>>> For your reference , here is the code of how the journal activity uses
>>> this data---- http://pastebin.be/20177 , but as I don't want to use
>>> hippo canvas, so this method won't work. Kindly give your suggestions.
>>
>> Sorry, didn't remembered that the journal used hippo to display the
>> previews when I recommended you to look at that.
>>
>> If you get a cairo surface like the journal does, you can draw it to a
>> pixmap and then tell a gtk.Image to display it. More details in this
>> post (disregard the pixbuf stuff):
>>
>> http://lethalman.blogspot.com/2009/04/create-pixbuf-from-cairo-surface.html
>>
>> HTH,
>>
>> Tomeu
>>
>>> Regards,
>>> sumit
>>>
>>
>


More information about the Sugar-devel mailing list