Bluetarget
Posts: 13
Joined: Wed Aug 10, 2016 5:16 pm

Screenshot with GTK : Always same image ?

Wed Aug 10, 2016 5:24 pm

Hello !

I'm currently trying to develop some recorder, and I need a *fast* way to grab screenshots (of the entire screen).

Although pyautogui did this very well (with PIL), it was really slow.

So I found a solution using gtk : 10 times faster :)

Here is the way I installed GTK for python :
sudo apt-get install libgtk-3-dev
sudo apt-get install python-gtk2
(I tested a few demo codes and everything worked fine)

Unfortunately, I'm afraid there is some bug I can't understand;
My code below is quite simple, but it always retrieve the same image...
I tested writing directly the buffer from GTK to some txt file to see if the problem came from any later code, but since the first screenshot the program keeps retrieving the same screenshot, even if the screen change or anything.
I think of some refresh problem in the GTK side... Ideas ?

Code: Select all

from gi.repository import GdkX11,Gdk

def image_grab_gtk(params):
	left, top, right, bot = params
	w = right - left
	h = bot - top

#Got the screen pointer for GTK
	window = Gdk.get_default_root_window()
#Retrieve pixel buffer from this screen
	pb = Gdk.pixbuf_get_from_window(window, 0, 0, w, h)

#Converts the buffer to some RGB image format
	final = Image.frombuffer( "RGB", (w, h), pb.get_pixels(), "raw", "RGB", pb.get_rowstride(), 1)
	
	return final
Thanks for any help you could provide :)

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Screenshot with GTK : Always same image ?

Fri Aug 12, 2016 6:54 pm

Apple say... Monkey do !!

Bluetarget
Posts: 13
Joined: Wed Aug 10, 2016 5:16 pm

Re: Screenshot with GTK : Always same image ?

Tue Aug 23, 2016 5:20 pm

Hello Davef21370, and thanks for your link.
Unfortunately, adding
while gtk.events_pending():
gtk.main_iteration(False)
does not change anything.

Return to “Python”