Skip to content

Commit 0a272cc

Browse files
committed
Do not resize macOS retina screenshots by default
1 parent 76f04b4 commit 0a272cc

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

Tests/test_imagegrab.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@ def test_grab(self) -> None:
2525
ImageGrab.grab(include_layered_windows=True)
2626
ImageGrab.grab(all_screens=True)
2727

28-
im = ImageGrab.grab(bbox=(10, 20, 50, 80))
29-
assert im.size == (40, 60)
28+
if sys.platform == "darwin":
29+
im = ImageGrab.grab(bbox=(10, 20, 50, 80))
30+
assert im.size in ((40, 60), (80, 120))
31+
32+
im = ImageGrab.grab(bbox=(10, 20, 50, 80), scale_down=True)
33+
assert im.size == (40, 60)
34+
else:
35+
im = ImageGrab.grab(bbox=(10, 20, 50, 80))
36+
assert im.size == (40, 60)
3037

3138
@skip_unless_feature("xcb")
3239
def test_grab_x11(self) -> None:

docs/reference/ImageGrab.rst

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ or the clipboard to a PIL image memory.
99

1010
.. versionadded:: 1.1.3
1111

12-
.. py:function:: grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=None, window=None)
12+
.. py:function:: grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=None, window=None, scale_down=False)
1313
1414
Take a snapshot of the screen. The pixels inside the bounding box are returned as
15-
an "RGBA" on macOS, or an "RGB" image otherwise. If the bounding box is omitted,
16-
the entire screen is copied, and on macOS, it will be at 2x if on a Retina screen.
15+
"RGBA" on macOS, or "RGB" image otherwise. If the bounding box is omitted,
16+
the entire screen is copied.
17+
18+
On macOS, it will be at 2x if on a Retina screen. If this is not desired, pass
19+
``scale_down=True``.
1720

1821
On Linux, if ``xdisplay`` is ``None`` and the default X11 display does not return
1922
a snapshot of the screen, ``gnome-screenshot``, ``grim`` or ``spectacle`` will be
@@ -25,8 +28,8 @@ or the clipboard to a PIL image memory.
2528
.. versionadded:: 7.1.0 Linux support
2629

2730
:param bbox: What region to copy. Default is the entire screen.
28-
On macOS, this is not increased to 2x for Retina screens, so the full
29-
width of a Retina screen would be 1440, not 2880.
31+
On macOS, this is increased to 2x for Retina screens, so the full
32+
width of a Retina screen would be 2880, not 1440.
3033
On Windows, the top-left point may be negative if ``all_screens=True``
3134
is used.
3235
:param include_layered_windows: Includes layered windows. Windows OS only.
@@ -47,6 +50,11 @@ or the clipboard to a PIL image memory.
4750
HWND, to capture a single window. Windows only.
4851

4952
.. versionadded:: 11.2.1
53+
54+
:param scale_down: On macOS, Retina screens will provide images at 2x size by default. This will prevent that, and scale down to 1x.
55+
Keyword-only argument.
56+
57+
.. versionadded:: 12.1.0
5058
:return: An image
5159

5260
.. py:function:: grabclipboard()

src/PIL/ImageGrab.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def grab(
3636
all_screens: bool = False,
3737
xdisplay: str | None = None,
3838
window: int | ImageWin.HWND | None = None,
39+
*,
40+
scale_down: bool = False,
3941
) -> Image.Image:
4042
im: Image.Image
4143
if xdisplay is None:
@@ -50,7 +52,7 @@ def grab(
5052
im = Image.open(filepath)
5153
im.load()
5254
os.unlink(filepath)
53-
if bbox:
55+
if bbox and scale_down:
5456
im_resized = im.resize((right - left, bottom - top))
5557
im.close()
5658
return im_resized

0 commit comments

Comments
 (0)