@@ -45,18 +45,37 @@ def grab(
45
45
args = ["screencapture" ]
46
46
if window :
47
47
args += ["-l" , str (window )]
48
- # -R is not working with -l
49
- if bbox and not window :
48
+ elif bbox :
50
49
left , top , right , bottom = bbox
51
50
args += ["-R" , f"{ left } ,{ top } ,{ right - left } ,{ bottom - top } " ]
52
51
subprocess .call (args + ["-x" , filepath ])
53
52
im = Image .open (filepath )
54
53
im .load ()
55
54
os .unlink (filepath )
56
55
if bbox :
57
- # manual crop for windowed mode
58
56
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 )
60
79
im .close ()
61
80
return im_cropped
62
81
else :
0 commit comments