Skip to content

Commit

Permalink
Added option to include layered windows in ImageGrab.grab on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Apr 26, 2019
1 parent c23d423 commit 70038bd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
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

0 comments on commit 70038bd

Please sign in to comment.