-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCaptureScreenMac.py
More file actions
44 lines (36 loc) · 1.67 KB
/
CaptureScreenMac.py
File metadata and controls
44 lines (36 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import Quartz
import Quartz.CoreGraphics as CG
from PIL import Image
import numpy as np
def capture_screen(window_title):
# Get the list of all windows
window_list = Quartz.CGWindowListCopyWindowInfo(Quartz.kCGWindowListOptionOnScreenOnly, Quartz.kCGNullWindowID)
# Find the window with the specified title
window = None
for win in window_list:
if win.get('kCGWindowName', '') == window_title:
window = win
break
if not window:
print(f"Window with title '{window_title}' not found")
return
# Get the window bounds
bounds = window['kCGWindowBounds']
x, y, width, height = int(bounds['X']), int(bounds['Y']), int(bounds['Width']), int(bounds['Height'])
# Create a screen capture of the specified area
image_ref = Quartz.CGWindowListCreateImage(CG.CGRectMake(x, y, width, height),
Quartz.kCGWindowListOptionIncludingWindow,
window['kCGWindowNumber'],
Quartz.kCGWindowImageBoundsIgnoreFraming)
if image_ref is None:
print(f"Failed to capture the window with title '{window_title}'.")
return None
# Get the actual width and height of the image
width = Quartz.CGImageGetWidth(image_ref)
height = Quartz.CGImageGetHeight(image_ref)
bytesperrow = CG.CGImageGetBytesPerRow(image_ref)
pixeldata = CG.CGDataProviderCopyData(CG.CGImageGetDataProvider(image_ref))
image = np.frombuffer(pixeldata, dtype=np.uint8)
image = image.reshape((height, bytesperrow//4, 4))
image = image[:,:width,:]
return image