Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to include layered windows in ImageGrab.grab on Windows #3808

Merged
merged 1 commit into from May 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions Tests/test_imagegrab.py
Expand Up @@ -9,8 +9,11 @@
class TestImageGrab(PillowTestCase):

def test_grab(self):
im = ImageGrab.grab()
self.assert_image(im, im.mode, im.size)
for im in [
ImageGrab.grab(),
ImageGrab.grab(include_layered_windows=True)
]:
self.assert_image(im, im.mode, im.size)

def test_grabclipboard(self):
if sys.platform == "darwin":
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/ImageGrab.rst
Expand Up @@ -11,7 +11,7 @@ or the clipboard to a PIL image memory.

.. versionadded:: 1.1.3

.. py:function:: PIL.ImageGrab.grab(bbox=None)
.. py:function:: PIL.ImageGrab.grab(bbox=None, include_layered_windows=False)

Take a snapshot of the screen. The pixels inside the bounding box are
returned as an "RGB" image on Windows or "RGBA" on macOS.
Expand All @@ -20,6 +20,7 @@ or the clipboard to a PIL image memory.
.. versionadded:: 1.1.3 (Windows), 3.0.0 (macOS)

:param bbox: What region to copy. Default is the entire screen.
:param include_layered_windows: Includes layered windows. Windows OS only.
:return: An image

.. py:function:: PIL.ImageGrab.grabclipboard()
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/ImageGrab.py
Expand Up @@ -29,7 +29,7 @@
import subprocess


def grab(bbox=None):
def grab(bbox=None, include_layered_windows=False):
if sys.platform == "darwin":
fh, filepath = tempfile.mkstemp('.png')
os.close(fh)
Expand All @@ -38,7 +38,7 @@ def grab(bbox=None):
im.load()
os.unlink(filepath)
else:
size, data = grabber()
size, data = grabber(include_layered_windows)
im = Image.frombytes(
"RGB", size, data,
# RGB, 32-bit line padding, origin lower left corner
Expand Down
10 changes: 9 additions & 1 deletion src/display.c
Expand Up @@ -323,11 +323,16 @@ PyObject*
PyImaging_GrabScreenWin32(PyObject* self, PyObject* args)
{
int width, height;
int includeLayeredWindows = 0;
HBITMAP bitmap;
BITMAPCOREHEADER core;
HDC screen, screen_copy;
DWORD rop;
PyObject* buffer;

if (!PyArg_ParseTuple(args, "|i", &includeLayeredWindows))
return NULL;

/* step 1: create a memory DC large enough to hold the
entire screen */

Expand All @@ -346,7 +351,10 @@ PyImaging_GrabScreenWin32(PyObject* self, PyObject* args)

/* step 2: copy bits into memory DC bitmap */

if (!BitBlt(screen_copy, 0, 0, width, height, screen, 0, 0, SRCCOPY))
rop = SRCCOPY;
if (includeLayeredWindows)
rop |= CAPTUREBLT;
if (!BitBlt(screen_copy, 0, 0, width, height, screen, 0, 0, rop))
goto error;

/* step 3: extract bits from bitmap */
Expand Down