prefer screenshots using XCB over gnome-screenshot

This commit is contained in:
nulano 2023-05-08 22:30:11 +01:00
parent 2a274a4760
commit bb18abc603
No known key found for this signature in database
GPG Key ID: B650CDF63B705766
2 changed files with 16 additions and 12 deletions

View File

@ -15,8 +15,9 @@ or the clipboard to a PIL image memory.
returned as an "RGBA" on macOS, or an "RGB" image otherwise. returned as an "RGBA" on macOS, or an "RGB" image otherwise.
If the bounding box is omitted, the entire screen is copied. If the bounding box is omitted, the entire screen is copied.
On Linux, if ``xdisplay`` is ``None`` then ``gnome-screenshot`` will be used if it On Linux, if ``xdisplay`` is ``None`` and the default X11 display does not return
is installed. To capture the default X11 display instead, pass ``xdisplay=""``. a snapshot of the screen, ``gnome-screenshot`` will be used as fallback if it is
installed. To disable this behaviour, pass ``xdisplay=""`` instead.
.. versionadded:: 1.1.3 (Windows), 3.0.0 (macOS), 7.1.0 (Linux) .. versionadded:: 1.1.3 (Windows), 3.0.0 (macOS), 7.1.0 (Linux)

View File

@ -61,7 +61,17 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=N
left, top, right, bottom = bbox left, top, right, bottom = bbox
im = im.crop((left - x0, top - y0, right - x0, bottom - y0)) im = im.crop((left - x0, top - y0, right - x0, bottom - y0))
return im return im
elif shutil.which("gnome-screenshot"): try:
if not Image.core.HAVE_XCB:
msg = "Pillow was built without XCB support"
raise OSError(msg)
size, data = Image.core.grabscreen_x11(xdisplay)
im = Image.frombytes("RGB", size, data, "raw", "BGRX", size[0] * 4, 1)
if bbox:
im = im.crop(bbox)
return im
except OSError:
if xdisplay is None and shutil.which("gnome-screenshot"):
fh, filepath = tempfile.mkstemp(".png") fh, filepath = tempfile.mkstemp(".png")
os.close(fh) os.close(fh)
subprocess.call(["gnome-screenshot", "-f", filepath]) subprocess.call(["gnome-screenshot", "-f", filepath])
@ -73,15 +83,8 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=N
im.close() im.close()
return im_cropped return im_cropped
return im return im
# use xdisplay=None for default display on non-win32/macOS systems else:
if not Image.core.HAVE_XCB: raise
msg = "Pillow was built without XCB support"
raise OSError(msg)
size, data = Image.core.grabscreen_x11(xdisplay)
im = Image.frombytes("RGB", size, data, "raw", "BGRX", size[0] * 4, 1)
if bbox:
im = im.crop(bbox)
return im
def grabclipboard(): def grabclipboard():