Skip to content

Commit 5ce88db

Browse files
committed
feat(ImageGrab): enhance grab function to support window-based screenshot capturing on macOS
1 parent d80cf0e commit 5ce88db

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/PIL/ImageGrab.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,27 @@ def grab(
4343
fh, filepath = tempfile.mkstemp(".png")
4444
os.close(fh)
4545
args = ["screencapture"]
46-
if bbox:
46+
if window:
47+
args += ["-l", str(window)]
48+
# -R is not working with -l
49+
if bbox and not window:
4750
left, top, right, bottom = bbox
4851
args += ["-R", f"{left},{top},{right-left},{bottom-top}"]
4952
subprocess.call(args + ["-x", filepath])
5053
im = Image.open(filepath)
5154
im.load()
5255
os.unlink(filepath)
5356
if bbox:
54-
im_resized = im.resize((right - left, bottom - top))
55-
im.close()
56-
return im_resized
57+
# manual crop for windowed mode
58+
if window:
59+
left, top, right, bottom = bbox
60+
im_cropped = im.crop((left, top, right, bottom))
61+
im.close()
62+
return im_cropped
63+
else:
64+
im_resized = im.resize((right - left, bottom - top))
65+
im.close()
66+
return im_resized
5767
return im
5868
elif sys.platform == "win32":
5969
if window is not None:

0 commit comments

Comments
 (0)