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