save gtk.DrawingArea to file

2019-02-20 02:23发布

问题:

I want to save gtk.DrawingArea() object contents to jpeg file using PIL. Particularly I want add to this script possibility to make photo. I found how to save image to jpeg. All I need - get pixbuf object from gtk.DrawingArea() object. How can I do this?

回答1:

If you're not married to the idea of using gstreamer, you can use OpenCV instead. This post gives a great example using pygame.

However, you can get the pixbuf this way (and you don't even need PIL):

def get_picture(self, event, data):
    drawable = self.movie_window.window
    colormap = drawable.get_colormap()
    pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 0, 8, *drawable.get_size())
    pixbuf = pixbuf.get_from_drawable(drawable, colormap, 0,0,0,0, *drawable.get_size())
    pixbuf.save(r'somefile.png', 'png')
    pixbuf.save(r'somefile.jpeg', 'jpeg')

and then just create a button and bind the command to get_picture.

Of course, if I were writing this program I would actually grab the image to an Image in the first place and then draw it on the gtk.DrawingArea - that would allow you to do all sorts of fun things.



标签: python gtk pygtk