[Sugar-devel] help about the get_preview function of activity.Activity class to take screenshots

Tomeu Vizoso tomeu at sugarlabs.org
Tue Jul 21 04:03:34 EDT 2009


On Tue, Jul 21, 2009 at 01:08, sumit singh<sumit.coder at gmail.com> wrote:
> Martin and Tomeu,
>
> Martin, thanx a lot for such a detailed answer. I have got what you want to
> say. But the problem with me right now is that I think the get_preview
> function is available only in the latest release of sugar 0.84 because I am
> on 0.82, and my xo gives an error -- " Activity module does not have an
> attribute get_preview" . Is it so that the function has been added after
> 0.82 ? If yes, then is there any way on 0.82 build to take screenshots
> programatically.
>
> There is one strange thing which I am encountering right now. Though the log
> gives an error that get_preview function is not present but when I ran write
> 63.xo on the machine ( after replacing sugar.graphics.colorbutton with
> gtk.colorButton  in toolbar.py ) , closed it and then again tried to restart
> the activity from the journal , I was able to see the following preview
> which I am attaching with this mail, that was what was there on my activity
> screen before I closed it.
>
> Though I am not very much sure, but this should have happened because of the
> save function in activity.Activity class , here is the link-
> http://api.sugarlabs.org/sugar.activity.activity-pysrc.html , which calls
> the get_preview function of sugar, which is a bit contradictory, if we
> consider the result of the logs. Am I wrong in the way I am understanding
> this? Kindly give your comments.

You are asking good questions ;)

In Sugar < 0.84, the only way to get a screenshot of activities was
asking the X server for the current contents of the screen. This was
pretty slow and also required that the activity was covering the whole
screen during that operation (so the user couldn't invoke the frame,
nor switch activities, etc.).

In Sugar 0.84, we upgraded to a newer version of gtk+ that adds API
for redrawing a window in a pixbuf. This is faster and can be done at
any moment. But the abiword shipped with 0.84 did things a bit
differently so that gtk+ API didn't worked properly, then Martin
Sevior kindly implemented an additional function that did the job in
the meantime.

This is the slow method for retrieving previews as was implemented in
the s.g.w.Window class:

def get_canvas_screenshot(self):
    if not self.canvas:
        return None
    window = self.canvas.window
    width, height = window.get_size()
    screenshot = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,
has_alpha=False, bits_per_sample=8, width=width, height=height)
    screenshot.get_from_drawable(window, window.get_colormap(), 0, 0,
0, 0, width, height)
    return screenshot

Regards,

Tomeu

> Regards,
> sumit.
>
>
>
>
>
>
> On Mon, Jul 20, 2009 at 6:58 PM, Martin Sevior <msevior at gmail.com> wrote:
>>
>> Hi Sumit,
>>
>> I just fixed the example code in the pyabiword module of abiword to
>> give an example
>> of how this works.
>>
>> The important line in this example is:
>>
>>  i.props.pixbuf = abi.render_page_to_image(1)
>>
>> The converts page number 1 to a GdkPixbuf. Once you have a GdkPixbuf,
>> in this case in
>> a Gtk.Image you can convert it to whatever format you wish.
>>
>> From the PyGtk reference manual http://www.pygtk.org/docs/pygtk/
>>  you convert  this to an image with:
>>
>>
>> http://www.pygtk.org/docs/pygtk/class-gdkpixbuf.html#method-gdkpixbuf--save
>>
>> So if you wanted the GdkPixbuf directly you would do:
>>
>> PagePixbuf = abi.render_page_to_image(1)
>> PagePixbuf.save(filename,"png")
>>
>> Where "filename" is the name of the file you wish to store to.
>>
>> Cheers!
>>
>> Martin
>>
>> PS. here is test.py which shows the feature in action (thanks for the
>> patch tomeu!)
>>
>> ----------------------------------------------------------
>> #!/usr/bin/python
>>
>> import sys
>> import pygtk
>> pygtk.require('2.0')
>> import gtk
>> import abiword
>>
>> window = gtk.Window()
>> window.set_default_size(640, 480)
>> window.connect('delete-event', gtk.main_quit)
>>
>> box = gtk.VBox()
>> window.add(box)
>> box.show()
>>
>> abi = abiword.Canvas()
>> box.add(abi)
>> abi.show()
>>
>> window.show()
>>
>> b = gtk.Button('render page')
>> box.add(b)
>> b.show()
>>
>> i = gtk.Image()
>> box.add(i)
>> i.show()
>>
>> def _clicked_cb(widget, abi, i):
>>    i.props.pixbuf = abi.render_page_to_image(1)
>>
>> b.connect('clicked', lambda widget: _clicked_cb(widget, abi, i))
>>
>> gtk.main()
>> --------------------------------------------
>>
>> On Mon, Jul 20, 2009 at 10:43 PM, sumit singh<sumit.coder at gmail.com>
>> wrote:
>> > Hi everyone,
>> >
>> > Can anyone please guide me about the proper use of the get_preview
>> > function
>> > of the activity.Activity class of sugar. I had a look at its api, it
>> > seems
>> > to be returning a dictionary sort of thing which is called as the png
>> > image.
>> > What I want to do is to save this image somewhere in the datastore and
>> > show
>> > this image later to the user in my activity. So, what I want to ask is
>> > to
>> > how to load this information in the form of an image later on
>> > programatically. Can we load it using the gtk.image widget and what is
>> > the
>> > right procedure to save this information.
>> >
>> > Regards,
>> > sumit
>> >
>> > _______________________________________________
>> > Sugar-devel mailing list
>> > Sugar-devel at lists.sugarlabs.org
>> > http://lists.sugarlabs.org/listinfo/sugar-devel
>> >
>> >
>
>


More information about the Sugar-devel mailing list