Skip to content

Commit 79914ec

Browse files
radarhereyankeguo
authored andcommitted
Check for scaling in macOS windows
1 parent 7eaac3f commit 79914ec

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/PIL/ImageGrab.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,37 @@ def grab(
4545
args = ["screencapture"]
4646
if window:
4747
args += ["-l", str(window)]
48-
# -R is not working with -l
49-
if bbox and not window:
48+
elif bbox:
5049
left, top, right, bottom = bbox
5150
args += ["-R", f"{left},{top},{right-left},{bottom-top}"]
5251
subprocess.call(args + ["-x", filepath])
5352
im = Image.open(filepath)
5453
im.load()
5554
os.unlink(filepath)
5655
if bbox:
57-
# manual crop for windowed mode
5856
if window:
59-
im_cropped = im.crop(bbox)
57+
# Determine if the window was in retina mode or not
58+
# by capturing it without the shadow,
59+
# and checking how different the width is
60+
fh, filepath = tempfile.mkstemp(".png")
61+
os.close(fh)
62+
subprocess.call(
63+
["screencapture", "-l", str(window), "-o", "-x", filepath]
64+
)
65+
with Image.open(filepath) as im_no_shadow:
66+
retina = im.width - im_no_shadow.width > 100
67+
os.unlink(filepath)
68+
69+
# Since screencapture's -R does not work with -l,
70+
# crop the image manually
71+
if retina:
72+
left, top, right, bottom = bbox
73+
im_cropped = im.resize(
74+
(right - left, bottom - top),
75+
box=tuple(coord * 2 for coord in bbox),
76+
)
77+
else:
78+
im_cropped = im.crop(bbox)
6079
im.close()
6180
return im_cropped
6281
else:

0 commit comments

Comments
 (0)