From 607acbf95e8ee0a5900d3406324bffc77e02b49a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 5 Nov 2024 07:05:39 +1100 Subject: [PATCH 01/14] Allow window to be supplied for ImageGrab.grab() on Windows --- Tests/test_imagegrab.py | 5 +++++ docs/reference/ImageGrab.rst | 5 +++++ src/PIL/ImageGrab.py | 11 ++++++++- src/display.c | 43 ++++++++++++++++++++++++++++++------ 4 files changed, 56 insertions(+), 8 deletions(-) diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index 5cd510751..032dac8cc 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -57,6 +57,11 @@ class TestImageGrab: ImageGrab.grab(xdisplay="error.test:0.0") assert str(e.value).startswith("X connection failed") + @pytest.mark.skipif(sys.platform != "win32", reason="Windows only") + def test_grab_invalid_handle(self) -> None: + with pytest.raises(OSError): + ImageGrab.grab(window=-1) + def test_grabclipboard(self) -> None: if sys.platform == "darwin": subprocess.call(["screencapture", "-cx"]) diff --git a/docs/reference/ImageGrab.rst b/docs/reference/ImageGrab.rst index db2987eb0..6435e1a0c 100644 --- a/docs/reference/ImageGrab.rst +++ b/docs/reference/ImageGrab.rst @@ -39,6 +39,11 @@ or the clipboard to a PIL image memory. You can check X11 support using :py:func:`PIL.features.check_feature` with ``feature="xcb"``. .. versionadded:: 7.1.0 + + :param handle: + HWND, to capture a single window. Windows only. + + .. versionadded:: 11.1.0 :return: An image .. py:function:: grabclipboard() diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index e27ca7e50..4dcaaa6c3 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -22,15 +22,20 @@ import shutil import subprocess import sys import tempfile +from typing import TYPE_CHECKING from . import Image +if TYPE_CHECKING: + from . import ImageWin + def grab( bbox: tuple[int, int, int, int] | None = None, include_layered_windows: bool = False, all_screens: bool = False, xdisplay: str | None = None, + window: int | ImageWin.HWND | None = None, ) -> Image.Image: im: Image.Image if xdisplay is None: @@ -51,8 +56,12 @@ def grab( return im_resized return im elif sys.platform == "win32": + if window is not None: + all_screens = -1 offset, size, data = Image.core.grabscreen_win32( - include_layered_windows, all_screens + include_layered_windows, + all_screens, + int(window) if window is not None else 0, ) im = Image.frombytes( "RGB", diff --git a/src/display.c b/src/display.c index b4e2e3899..30b7ada11 100644 --- a/src/display.c +++ b/src/display.c @@ -320,25 +320,36 @@ typedef HANDLE(__stdcall *Func_SetThreadDpiAwarenessContext)(HANDLE); PyObject * PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { - int x = 0, y = 0, width, height; - int includeLayeredWindows = 0, all_screens = 0; + int x = 0, y = 0, width = -1, height; + int includeLayeredWindows = 0, screens = 0; HBITMAP bitmap; BITMAPCOREHEADER core; HDC screen, screen_copy; + HWND wnd; DWORD rop; PyObject *buffer; HANDLE dpiAwareness; HMODULE user32; Func_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContext_function; - if (!PyArg_ParseTuple(args, "|ii", &includeLayeredWindows, &all_screens)) { + if (!PyArg_ParseTuple( + args, "|ii" F_HANDLE, &includeLayeredWindows, &screens, &wnd + )) { return NULL; } /* step 1: create a memory DC large enough to hold the entire screen */ - screen = CreateDC("DISPLAY", NULL, NULL, NULL); + if (screens == -1) { + screen = GetDC(wnd); + if (screen == NULL) { + PyErr_SetString(PyExc_OSError, "unable to get device context for handle"); + return NULL; + } + } else { + screen = CreateDC("DISPLAY", NULL, NULL, NULL); + } screen_copy = CreateCompatibleDC(screen); // added in Windows 10 (1607) @@ -351,11 +362,17 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { dpiAwareness = SetThreadDpiAwarenessContext_function((HANDLE)-3); } - if (all_screens) { + if (screens == 1) { x = GetSystemMetrics(SM_XVIRTUALSCREEN); y = GetSystemMetrics(SM_YVIRTUALSCREEN); width = GetSystemMetrics(SM_CXVIRTUALSCREEN); height = GetSystemMetrics(SM_CYVIRTUALSCREEN); + } else if (screens == -1) { + RECT rect; + if (GetClientRect(wnd, &rect)) { + width = rect.right; + height = rect.bottom; + } } else { width = GetDeviceCaps(screen, HORZRES); height = GetDeviceCaps(screen, VERTRES); @@ -367,6 +384,10 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { FreeLibrary(user32); + if (width == -1) { + goto error; + } + bitmap = CreateCompatibleBitmap(screen, width, height); if (!bitmap) { goto error; @@ -412,7 +433,11 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { DeleteObject(bitmap); DeleteDC(screen_copy); - DeleteDC(screen); + if (screens == -1) { + ReleaseDC(wnd, screen); + } else { + DeleteDC(screen); + } return Py_BuildValue("(ii)(ii)N", x, y, width, height, buffer); @@ -420,7 +445,11 @@ error: PyErr_SetString(PyExc_OSError, "screen grab failed"); DeleteDC(screen_copy); - DeleteDC(screen); + if (screens == -1) { + ReleaseDC(wnd, screen); + } else { + DeleteDC(screen); + } return NULL; } From 28e5b929f8dcae3312f45e8aeb8d3b03b290036d Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 5 Nov 2024 08:40:09 +1100 Subject: [PATCH 02/14] Test 0 --- Tests/test_imagegrab.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index 032dac8cc..35aaa7ff0 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -59,8 +59,10 @@ class TestImageGrab: @pytest.mark.skipif(sys.platform != "win32", reason="Windows only") def test_grab_invalid_handle(self) -> None: - with pytest.raises(OSError): + with pytest.raises(OSError, match="unable to get device context for handle"): ImageGrab.grab(window=-1) + with pytest.raises(OSError, match="screen grab failed"): + ImageGrab.grab(window=0) def test_grabclipboard(self) -> None: if sys.platform == "darwin": From 9622266c2a9b81b7b9a9e2e670571f9889bbe4d5 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 5 Nov 2024 18:33:25 +1100 Subject: [PATCH 03/14] Use DPI awareness from window --- src/display.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/display.c b/src/display.c index 30b7ada11..b78d0dca1 100644 --- a/src/display.c +++ b/src/display.c @@ -316,6 +316,7 @@ PyImaging_DisplayModeWin32(PyObject *self, PyObject *args) { /* -------------------------------------------------------------------- */ /* Windows screen grabber */ +typedef HANDLE(__stdcall *Func_GetWindowDpiAwarenessContext)(HANDLE); typedef HANDLE(__stdcall *Func_SetThreadDpiAwarenessContext)(HANDLE); PyObject * @@ -330,6 +331,7 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { PyObject *buffer; HANDLE dpiAwareness; HMODULE user32; + Func_GetWindowDpiAwarenessContext GetWindowDpiAwarenessContext_function; Func_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContext_function; if (!PyArg_ParseTuple( @@ -358,8 +360,19 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { SetThreadDpiAwarenessContext_function = (Func_SetThreadDpiAwarenessContext )GetProcAddress(user32, "SetThreadDpiAwarenessContext"); if (SetThreadDpiAwarenessContext_function != NULL) { - // DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = ((DPI_CONTEXT_HANDLE)-3) - dpiAwareness = SetThreadDpiAwarenessContext_function((HANDLE)-3); + if (screens == -1) { + GetWindowDpiAwarenessContext_function = (Func_GetWindowDpiAwarenessContext + )GetProcAddress(user32, "GetWindowDpiAwarenessContext"); + DPI_AWARENESS_CONTEXT dpiAwarenessContext = + GetWindowDpiAwarenessContext_function(wnd); + if (dpiAwarenessContext != NULL) { + dpiAwareness = + SetThreadDpiAwarenessContext_function(dpiAwarenessContext); + } + } else { + // DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = ((DPI_CONTEXT_HANDLE)-3) + dpiAwareness = SetThreadDpiAwarenessContext_function((HANDLE)-3); + } } if (screens == 1) { From 7763350f072ca4ebe714352871df3ef8429b40bb Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 8 Nov 2024 07:30:09 +1100 Subject: [PATCH 04/14] Fallback to PER_MONITOR_AWARE --- src/display.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/display.c b/src/display.c index b78d0dca1..da248443c 100644 --- a/src/display.c +++ b/src/display.c @@ -368,6 +368,8 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { if (dpiAwarenessContext != NULL) { dpiAwareness = SetThreadDpiAwarenessContext_function(dpiAwarenessContext); + } else { + dpiAwareness = SetThreadDpiAwarenessContext_function((HANDLE)-3); } } else { // DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = ((DPI_CONTEXT_HANDLE)-3) From a44b3067b0e31d6c3a89ba270db20b1a1a71d226 Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Fri, 8 Nov 2024 07:45:29 +1100 Subject: [PATCH 05/14] Fallback to PER_MONITOR_AWARE if GetWindowDpiAwarenessContext is not available MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ondrej Baranovič --- src/display.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/display.c b/src/display.c index da248443c..f28926427 100644 --- a/src/display.c +++ b/src/display.c @@ -365,7 +365,7 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { )GetProcAddress(user32, "GetWindowDpiAwarenessContext"); DPI_AWARENESS_CONTEXT dpiAwarenessContext = GetWindowDpiAwarenessContext_function(wnd); - if (dpiAwarenessContext != NULL) { + if (GetWindowDpiAwarenessContext_function != NULL && dpiAwarenessContext != NULL) { dpiAwareness = SetThreadDpiAwarenessContext_function(dpiAwarenessContext); } else { From 288d77efd6f198d1c01c3eb62c333c6021b521bc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 Nov 2024 20:45:58 +0000 Subject: [PATCH 06/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/display.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/display.c b/src/display.c index f28926427..89fec2c28 100644 --- a/src/display.c +++ b/src/display.c @@ -365,7 +365,8 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { )GetProcAddress(user32, "GetWindowDpiAwarenessContext"); DPI_AWARENESS_CONTEXT dpiAwarenessContext = GetWindowDpiAwarenessContext_function(wnd); - if (GetWindowDpiAwarenessContext_function != NULL && dpiAwarenessContext != NULL) { + if (GetWindowDpiAwarenessContext_function != NULL && + dpiAwarenessContext != NULL) { dpiAwareness = SetThreadDpiAwarenessContext_function(dpiAwarenessContext); } else { From 4b8867069b3b49cfabe2a7ef6275a7c0d85406af Mon Sep 17 00:00:00 2001 From: Nulano Date: Thu, 7 Nov 2024 22:06:28 +0100 Subject: [PATCH 07/14] Fix GetWindowDpiAwarenessContext NULL check --- src/display.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/display.c b/src/display.c index 89fec2c28..5babd4f2a 100644 --- a/src/display.c +++ b/src/display.c @@ -329,7 +329,7 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { HWND wnd; DWORD rop; PyObject *buffer; - HANDLE dpiAwareness; + HANDLE dpiAwareness = NULL; HMODULE user32; Func_GetWindowDpiAwarenessContext GetWindowDpiAwarenessContext_function; Func_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContext_function; @@ -359,23 +359,15 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { user32 = LoadLibraryA("User32.dll"); SetThreadDpiAwarenessContext_function = (Func_SetThreadDpiAwarenessContext )GetProcAddress(user32, "SetThreadDpiAwarenessContext"); + GetWindowDpiAwarenessContext_function = (Func_GetWindowDpiAwarenessContext + )GetProcAddress(user32, "GetWindowDpiAwarenessContext"); if (SetThreadDpiAwarenessContext_function != NULL) { - if (screens == -1) { - GetWindowDpiAwarenessContext_function = (Func_GetWindowDpiAwarenessContext - )GetProcAddress(user32, "GetWindowDpiAwarenessContext"); - DPI_AWARENESS_CONTEXT dpiAwarenessContext = + if (screens == -1 && GetWindowDpiAwarenessContext_function != NULL) { + dpiAwareness = GetWindowDpiAwarenessContext_function(wnd); - if (GetWindowDpiAwarenessContext_function != NULL && - dpiAwarenessContext != NULL) { - dpiAwareness = - SetThreadDpiAwarenessContext_function(dpiAwarenessContext); - } else { - dpiAwareness = SetThreadDpiAwarenessContext_function((HANDLE)-3); - } - } else { - // DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = ((DPI_CONTEXT_HANDLE)-3) - dpiAwareness = SetThreadDpiAwarenessContext_function((HANDLE)-3); } + // DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = ((DPI_CONTEXT_HANDLE)-3) + dpiAwareness = SetThreadDpiAwarenessContext_function(dpiAwareness == NULL ? (HANDLE)-3 : dpiAwareness); } if (screens == 1) { From a6c941ac2c5e0b88d827c6d5c753cce3021e9e92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ondrej=20Baranovi=C4=8D?= Date: Thu, 7 Nov 2024 22:22:02 +0100 Subject: [PATCH 08/14] Do not load GetWindowDpiAwarenessContext until its needed Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- src/display.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/display.c b/src/display.c index 5babd4f2a..353d52396 100644 --- a/src/display.c +++ b/src/display.c @@ -359,9 +359,9 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { user32 = LoadLibraryA("User32.dll"); SetThreadDpiAwarenessContext_function = (Func_SetThreadDpiAwarenessContext )GetProcAddress(user32, "SetThreadDpiAwarenessContext"); - GetWindowDpiAwarenessContext_function = (Func_GetWindowDpiAwarenessContext - )GetProcAddress(user32, "GetWindowDpiAwarenessContext"); if (SetThreadDpiAwarenessContext_function != NULL) { + GetWindowDpiAwarenessContext_function = (Func_GetWindowDpiAwarenessContext + )GetProcAddress(user32, "GetWindowDpiAwarenessContext"); if (screens == -1 && GetWindowDpiAwarenessContext_function != NULL) { dpiAwareness = GetWindowDpiAwarenessContext_function(wnd); From acba5c47f8be16869da92efb877f07451612fa04 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 8 Nov 2024 08:24:13 +1100 Subject: [PATCH 09/14] Lint fix --- src/display.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/display.c b/src/display.c index 353d52396..b0693bf71 100644 --- a/src/display.c +++ b/src/display.c @@ -363,11 +363,12 @@ PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) { GetWindowDpiAwarenessContext_function = (Func_GetWindowDpiAwarenessContext )GetProcAddress(user32, "GetWindowDpiAwarenessContext"); if (screens == -1 && GetWindowDpiAwarenessContext_function != NULL) { - dpiAwareness = - GetWindowDpiAwarenessContext_function(wnd); + dpiAwareness = GetWindowDpiAwarenessContext_function(wnd); } // DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = ((DPI_CONTEXT_HANDLE)-3) - dpiAwareness = SetThreadDpiAwarenessContext_function(dpiAwareness == NULL ? (HANDLE)-3 : dpiAwareness); + dpiAwareness = SetThreadDpiAwarenessContext_function( + dpiAwareness == NULL ? (HANDLE)-3 : dpiAwareness + ); } if (screens == 1) { From e053be3412ea6db562cd4f240471e79f6bdbae53 Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Sun, 30 Mar 2025 07:27:30 +1100 Subject: [PATCH 10/14] Updated version Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- docs/reference/ImageGrab.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference/ImageGrab.rst b/docs/reference/ImageGrab.rst index 6435e1a0c..671d1ccee 100644 --- a/docs/reference/ImageGrab.rst +++ b/docs/reference/ImageGrab.rst @@ -43,7 +43,7 @@ or the clipboard to a PIL image memory. :param handle: HWND, to capture a single window. Windows only. - .. versionadded:: 11.1.0 + .. versionadded:: 11.2.0 :return: An image .. py:function:: grabclipboard() From 80d5b421ebd8b8c20c39889862764822366ef183 Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Sun, 30 Mar 2025 22:13:21 +1100 Subject: [PATCH 11/14] Do not import type checking Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- src/PIL/ImageGrab.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 0113ebcbf..e978b5d23 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -22,10 +22,9 @@ import shutil import subprocess import sys import tempfile -from typing import TYPE_CHECKING - from . import Image +TYPE_CHECKING = False if TYPE_CHECKING: from . import ImageWin From d2683e052f341a5f53c7bb440af7fba577e0f4d1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 30 Mar 2025 11:13:48 +0000 Subject: [PATCH 12/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/PIL/ImageGrab.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index e978b5d23..42abdf8c7 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -22,6 +22,7 @@ import shutil import subprocess import sys import tempfile + from . import Image TYPE_CHECKING = False From b4a480ff2cc3e418c04993a54f43b16df9174c28 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 31 Mar 2025 00:31:56 +1100 Subject: [PATCH 13/14] Corrected documentation --- docs/reference/ImageGrab.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/reference/ImageGrab.rst b/docs/reference/ImageGrab.rst index 671d1ccee..54d66db13 100644 --- a/docs/reference/ImageGrab.rst +++ b/docs/reference/ImageGrab.rst @@ -9,7 +9,7 @@ or the clipboard to a PIL image memory. .. versionadded:: 1.1.3 -.. py:function:: grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=None) +.. py:function:: grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=None, window=None) Take a snapshot of the screen. The pixels inside the bounding box are returned as an "RGBA" on macOS, or an "RGB" image otherwise. If the bounding box is omitted, @@ -40,7 +40,7 @@ or the clipboard to a PIL image memory. .. versionadded:: 7.1.0 - :param handle: + :param window: HWND, to capture a single window. Windows only. .. versionadded:: 11.2.0 From 25af4f1841c837c8e7ad370f4b001409c1b221c2 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 31 Mar 2025 00:32:35 +1100 Subject: [PATCH 14/14] Added release notes --- docs/releasenotes/11.2.0.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/releasenotes/11.2.0.rst b/docs/releasenotes/11.2.0.rst index d40d86f21..9f27d8456 100644 --- a/docs/releasenotes/11.2.0.rst +++ b/docs/releasenotes/11.2.0.rst @@ -51,6 +51,15 @@ aligned using ``"justify"`` in :py:mod:`~PIL.ImageDraw`:: draw.multiline_text((0, 0), "Multiline\ntext 1", align="justify") draw.multiline_textbbox((0, 0), "Multiline\ntext 2", align="justify") +Specify window in ImageGrab on Windows +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +When using :py:meth:`~PIL.ImageGrab.grab`, a specific window can be selected using the +HWND:: + + from PIL import ImageGrab + ImageGrab.grab(window=hwnd) + Check for MozJPEG ^^^^^^^^^^^^^^^^^