Do not use gnome-screenshot on Linux if X11 can be used instead

This commit is contained in:
Andrew Murray 2023-05-06 19:06:08 +10:00
parent 2a274a4760
commit fc24642535
2 changed files with 6 additions and 3 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 X11 is in use and Pillow was built with XCB support, libxcb will be
is installed. To capture the default X11 display instead, pass ``xdisplay=""``. used. Otherwise, if no ``xdisplay`` has been specified, ``gnome-screenshot`` will
be used if it is installed.
.. 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,9 @@ 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"): elif not (
Image.core.HAVE_XCB and os.environ.get("XDG_SESSION_TYPE") == "x11"
) 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])