xcb screengrab docs and fixes

This commit is contained in:
nulano 2019-12-09 15:55:15 +01:00 committed by Andrew Murray
parent f9c74825a6
commit 0bcc7be89b
3 changed files with 22 additions and 15 deletions

View File

@ -46,9 +46,6 @@ class TestImageGrab:
assert isinstance(exception, IOError)
assert str(exception).startswith("X connection failed")
@pytest.mark.skipif(
sys.platform not in ("win32", "darwin"), reason="requires Windows or macOS"
)
def test_grabclipboard(self):
if sys.platform == "darwin":
subprocess.call(["screencapture", "-cx"])
@ -62,7 +59,15 @@ $bmp = New-Object Drawing.Bitmap 200, 200
)
p.communicate()
else:
pytest.skip("ImageGrab.grabclipboard() is macOS and Windows only")
exception = None
try:
ImageGrab.grabclipboard()
except IOError as e:
exception = e
assert isinstance(exception, IOError)
assert str(exception) == "ImageGrab.grabclipboard() is macOS and Windows only"
return
im = ImageGrab.grabclipboard()

View File

@ -11,13 +11,13 @@ or the clipboard to a PIL image memory.
.. versionadded:: 1.1.3
.. py:function:: PIL.ImageGrab.grab(bbox=None, include_layered_windows=False, all_screens=False)
.. py:function:: PIL.ImageGrab.grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=None)
Take a snapshot of the screen. The pixels inside the bounding box are
returned as an "RGB" image on Windows or "RGBA" on macOS.
If the bounding box is omitted, the entire screen is copied.
.. versionadded:: 1.1.3 (Windows), 3.0.0 (macOS)
.. versionadded:: 1.1.3 (Windows), 3.0.0 (macOS), 7.0.0 (Linux (X11))
:param bbox: What region to copy. Default is the entire screen.
Note that on Windows OS, the top-left point may be negative if ``all_screens=True`` is used.
@ -27,6 +27,11 @@ or the clipboard to a PIL image memory.
:param all_screens: Capture all monitors. Windows OS only.
.. versionadded:: 6.2.0
:param xdisplay: X11 Display address. Pass ``None`` to grab the default system screen.
Pass ``""`` to grab the default X11 screen on Windows or macOS.
.. versionadded:: 7.0.0
:return: An image
.. py:function:: PIL.ImageGrab.grabclipboard()

View File

@ -15,10 +15,7 @@
# See the README file for information on usage and redistribution.
#
import os
import subprocess
import sys
import tempfile
from . import Image
@ -26,10 +23,6 @@ if sys.platform == "darwin":
import os
import tempfile
import subprocess
elif sys.platform == "win32":
pass
elif not Image.core.HAVE_XCB:
raise ImportError("ImageGrab requires Windows, macOS, or the XCB library")
def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=None):
@ -47,7 +40,9 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=N
return im_cropped
return im
elif sys.platform == "win32":
offset, size, data = Image.core.grabscreen(include_layered_windows, all_screens)
offset, size, data = Image.core.grabscreen_win32(
include_layered_windows, all_screens
)
im = Image.frombytes(
"RGB",
size,
@ -65,7 +60,7 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=N
return im
# use xdisplay=None for default display on non-win32/macOS systems
if not Image.core.HAVE_XCB:
raise AttributeError("XCB support not present")
raise IOError("Pillow was built without XCB support")
size, data = Image.core.grabscreen_x11(xdisplay)
im = Image.frombytes("RGB", size, data, "raw", "BGRX", size[0] * 4, 1)
if bbox:
@ -105,3 +100,5 @@ def grabclipboard():
return BmpImagePlugin.DibImageFile(io.BytesIO(data))
return data
else:
raise IOError("ImageGrab.grabclipboard() is macOS and Windows only")