Skip to content

Commit 34ff170

Browse files
authored
Merge pull request #7143 from nulano/imagegrab-prefer-xcb
Prefer screenshots using XCB over gnome-screenshot
2 parents baad0ad + 7e29efd commit 34ff170

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

Diff for: docs/reference/ImageGrab.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ or the clipboard to a PIL image memory.
1515
returned as an "RGBA" on macOS, or an "RGB" image otherwise.
1616
If the bounding box is omitted, the entire screen is copied.
1717

18-
On Linux, if ``xdisplay`` is ``None`` then ``gnome-screenshot`` will be used if it
19-
is installed. To capture the default X11 display instead, pass ``xdisplay=""``.
18+
On Linux, if ``xdisplay`` is ``None`` and the default X11 display does not return
19+
a snapshot of the screen, ``gnome-screenshot`` will be used as fallback if it is
20+
installed. To disable this behaviour, pass ``xdisplay=""`` instead.
2021

2122
.. versionadded:: 1.1.3 (Windows), 3.0.0 (macOS), 7.1.0 (Linux)
2223

Diff for: src/PIL/ImageGrab.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,17 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=N
6161
left, top, right, bottom = bbox
6262
im = im.crop((left - x0, top - y0, right - x0, bottom - y0))
6363
return im
64-
elif shutil.which("gnome-screenshot"):
64+
try:
65+
if not Image.core.HAVE_XCB:
66+
msg = "Pillow was built without XCB support"
67+
raise OSError(msg)
68+
size, data = Image.core.grabscreen_x11(xdisplay)
69+
except OSError:
70+
if (
71+
xdisplay is None
72+
and sys.platform not in ("darwin", "win32")
73+
and shutil.which("gnome-screenshot")
74+
):
6575
fh, filepath = tempfile.mkstemp(".png")
6676
os.close(fh)
6777
subprocess.call(["gnome-screenshot", "-f", filepath])
@@ -73,15 +83,13 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=N
7383
im.close()
7484
return im_cropped
7585
return im
76-
# use xdisplay=None for default display on non-win32/macOS systems
77-
if not Image.core.HAVE_XCB:
78-
msg = "Pillow was built without XCB support"
79-
raise OSError(msg)
80-
size, data = Image.core.grabscreen_x11(xdisplay)
81-
im = Image.frombytes("RGB", size, data, "raw", "BGRX", size[0] * 4, 1)
82-
if bbox:
83-
im = im.crop(bbox)
84-
return im
86+
else:
87+
raise
88+
else:
89+
im = Image.frombytes("RGB", size, data, "raw", "BGRX", size[0] * 4, 1)
90+
if bbox:
91+
im = im.crop(bbox)
92+
return im
8593

8694

8795
def grabclipboard():

0 commit comments

Comments
 (0)