Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyGTK+3 (PyGObject) to create screenshot?

I've spend past 3 days searching in google, how can I create a screenshot with PyGTK+3 ? There are gallizion tutorials about pyqt,pygtk+2,wx and PIL.

By the way, I don't need external programs like scrot, imlib2, imagemagick and so on.

like image 896
HexSFd Avatar asked Oct 21 '25 01:10

HexSFd


2 Answers

Since nobody else posted the translation to GTK3, here you go:

from gi.repository import Gdk

win = Gdk.get_default_root_window()
h = win.get_height()
w = win.get_width()

print ("The size of the window is %d x %d" % (w, h))

pb = Gdk.pixbuf_get_from_window(win, 0, 0, w, h)

if (pb != None):
    pb.savev("screenshot.png","png", (), ())
    print("Screenshot saved to screenshot.png.")
else:
    print("Unable to get the screenshot.")
like image 140
gianmt Avatar answered Oct 22 '25 16:10

gianmt


Better later than never. I got stuck with get_from_drawable() but later found the documentation about its deprecation.

from gi.repository import Gdk

window = Gdk.get_default_root_window()
x, y, width, height = window.get_geometry()

print("The size of the root window is {} x {}".format(width, height))

# get_from_drawable() was deprecated. See:
# https://developer.gnome.org/gtk3/stable/ch24s02.html#id-1.6.3.4.7
pb = Gdk.pixbuf_get_from_window(window, x, y, width, height)

if pb:
    pb.savev("screenshot.png", "png", (), ())
    print("Screenshot saved to screenshot.png.")
else:
    print("Unable to get the screenshot.")
like image 36
Havok Avatar answered Oct 22 '25 15:10

Havok