Skip to content

Commit

Permalink
Merge pull request #7143 from nulano/imagegrab-prefer-xcb
Browse files Browse the repository at this point in the history
Prefer screenshots using XCB over gnome-screenshot
  • Loading branch information
hugovk authored May 14, 2023
2 parents baad0ad + 7e29efd commit 34ff170
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
5 changes: 3 additions & 2 deletions docs/reference/ImageGrab.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ or the clipboard to a PIL image memory.
returned as an "RGBA" on macOS, or an "RGB" image otherwise.
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
is installed. To capture the default X11 display instead, pass ``xdisplay=""``.
On Linux, if ``xdisplay`` is ``None`` and the default X11 display does not return
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)

Expand Down
28 changes: 18 additions & 10 deletions src/PIL/ImageGrab.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,17 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=N
left, top, right, bottom = bbox
im = im.crop((left - x0, top - y0, right - x0, bottom - y0))
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)
except OSError:
if (
xdisplay is None
and sys.platform not in ("darwin", "win32")
and shutil.which("gnome-screenshot")
):
fh, filepath = tempfile.mkstemp(".png")
os.close(fh)
subprocess.call(["gnome-screenshot", "-f", filepath])
Expand All @@ -73,15 +83,13 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=N
im.close()
return im_cropped
return im
# use xdisplay=None for default display on non-win32/macOS systems
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
else:
raise
else:
im = Image.frombytes("RGB", size, data, "raw", "BGRX", size[0] * 4, 1)
if bbox:
im = im.crop(bbox)
return im


def grabclipboard():
Expand Down

0 comments on commit 34ff170

Please sign in to comment.