From dd2b91c5b8fe42eaa88600a857525a4f05f5e910 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 20 Mar 2022 12:59:10 +1100 Subject: [PATCH] Use screencapture arguments to crop on macOS --- src/PIL/ImageGrab.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index b93ec3f2a6c..8bd14d33191 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -30,14 +30,18 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=N if sys.platform == "darwin": fh, filepath = tempfile.mkstemp(".png") os.close(fh) - subprocess.call(["screencapture", "-x", filepath]) + args = ["screencapture"] + if bbox: + left, top, right, bottom = bbox + args += ["-R", f"{left},{right},{right-left},{bottom-top}"] + subprocess.call(args + ["-x", filepath]) im = Image.open(filepath) im.load() os.unlink(filepath) if bbox: - im_cropped = im.crop(bbox) + im_resized = im.resize((right - left, bottom - top)) im.close() - return im_cropped + return im_resized return im elif sys.platform == "win32": offset, size, data = Image.core.grabscreen_win32(