ponty / pyscreenshot

Python screenshot library, replacement for the Pillow ImageGrab module on Linux.
BSD 2-Clause "Simplified" License
499 stars 89 forks source link

pygdk3 backend, using Gdk 3.0+, compatible with Python3 and pretty fa… #53

Closed gatoatigrado closed 5 years ago

gatoatigrado commented 5 years ago

…st (~70ms for 4K screenshot)

Fixes / avoids https://github.com/ponty/pyscreenshot/issues/43

N.B. you must use childprocess=False to get good performance!! Unfortunately default Python inter-thread communication/forking/etc. is painfully slow. Compare:

In [5]: %time pyscreenshot.grab(backend='pygdk3')
CPU times: user 6.38 ms, sys: 0 ns, total: 6.38 ms
Wall time: 397 ms
Out[5]: <PIL.PngImagePlugin.PngImageFile image mode=RGB size=3840x2160 at 0x7F9721DA4908>

In [7]: %time pyscreenshot.grab(backend='pygdk3', childprocess=False)
CPU times: user 59.1 ms, sys: 27.9 ms, total: 87 ms
Wall time: 102 ms
Out[7]: <PIL.Image.Image image mode=RGB size=3840x2160 at 0x7F970E57FF28>

In [8]: %time pyscreenshot.grab(backend='pygdk3', childprocess=False)
CPU times: user 36.7 ms, sys: 12.1 ms, total: 48.8 ms
Wall time: 67.9 ms
Out[8]: <PIL.Image.Image image mode=RGB size=3840x2160 at 0x7F970E5841D0>

In [9]: %time pyscreenshot.grab(backend='pygdk3', childprocess=False)
CPU times: user 26.9 ms, sys: 19.4 ms, total: 46.2 ms
Wall time: 65.3 ms
Out[9]: <PIL.Image.Image image mode=RGB size=3840x2160 at 0x7F970E584550>

Backend speedtest (which does not use childprocess=False) results:

n=10                                                                                             
------------------------------------------------------                     
pygdk3                  3.8  sec        (  383 ms per call)
pyqt5                   7.4  sec        (  741 ms per call)
imagemagick             11   sec        ( 1062 ms per call)
gnome-screenshot        8.8  sec        (  883 ms per call)

but with childprocess=False it is an order of magnitude faster than other backends ^_^. Sorry I don't have scrot/pyside/pyqt/wx/pil backends installed to compare.

coveralls commented 5 years ago

Coverage Status

Coverage decreased (-0.3%) to 38.271% when pulling c6e0e25a73580620635dee0237393b445ad17cc2 on gatoatigrado:master into 0079fae23dfa36180ec0d4121286baecc24a708d on ponty:master.

coveralls commented 5 years ago

Coverage Status

Coverage decreased (-0.3%) to 38.271% when pulling c6e0e25a73580620635dee0237393b445ad17cc2 on gatoatigrado:master into 0079fae23dfa36180ec0d4121286baecc24a708d on ponty:master.

coveralls commented 5 years ago

Coverage Status

Coverage decreased (-0.3%) to 38.271% when pulling c6e0e25a73580620635dee0237393b445ad17cc2 on gatoatigrado:master into 0079fae23dfa36180ec0d4121286baecc24a708d on ponty:master.

coveralls commented 5 years ago

Coverage Status

Coverage decreased (-0.4%) to 38.159% when pulling 40233cf17492f2f6b2f7e0950ac42317e9487b40 on gatoatigrado:master into 0079fae23dfa36180ec0d4121286baecc24a708d on ponty:master.

gatoatigrado commented 5 years ago

Sorry I was a bit overzealous, and did not handle bboxes correctly ... it seems gdk3 pixbufs have a few empty values at the end of each line (probably for SSE alignment). Hopefully the new version is good! Performance still looks nominal, at least for my setup.

ponty commented 5 years ago

Thanks