1
1
import sys
2
+ import ctypes
3
+ from ctypes import c_void_p , c_double , c_uint32
2
4
from typing import Tuple
3
5
4
6
from je_auto_control .utils .exception .exception_tags import osx_import_error
@@ -15,3 +17,62 @@ def size() -> Tuple[int, int]:
15
17
get screen size
16
18
"""
17
19
return Quartz .CGDisplayPixelsWide ((Quartz .CGMainDisplayID ())), Quartz .CGDisplayPixelsHigh (Quartz .CGMainDisplayID ())
20
+
21
+ def get_pixel (x : int , y : int ) -> Tuple [int , int , int , int ]:
22
+ # Load CoreGraphics and CoreFoundation frameworks
23
+ cg = ctypes .CDLL ("/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics" )
24
+ cf = ctypes .CDLL ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation" )
25
+
26
+ # Define CGRect structure as 4 doubles: x, y, width, height
27
+ CGRect = ctypes .c_double * 4
28
+
29
+ # Function signatures
30
+ cg .CGWindowListCreateImage .argtypes = [CGRect , c_uint32 , c_uint32 , c_uint32 ]
31
+ cg .CGWindowListCreateImage .restype = c_void_p
32
+
33
+ cg .CGImageGetDataProvider .argtypes = [c_void_p ]
34
+ cg .CGImageGetDataProvider .restype = c_void_p
35
+
36
+ cg .CGDataProviderCopyData .argtypes = [c_void_p ]
37
+ cg .CGDataProviderCopyData .restype = c_void_p
38
+
39
+ cf .CFDataGetLength .argtypes = [c_void_p ]
40
+ cf .CFDataGetLength .restype = ctypes .c_long
41
+
42
+ cf .CFDataGetBytePtr .argtypes = [c_void_p ]
43
+ cf .CFDataGetBytePtr .restype = ctypes .POINTER (ctypes .c_ubyte )
44
+
45
+ cf .CFRelease .argtypes = [c_void_p ]
46
+ cf .CFRelease .restype = None
47
+
48
+ # Constants
49
+ kCGWindowListOptionOnScreenOnly = 1
50
+ kCGNullWindowID = 0
51
+ kCGWindowImageDefault = 0
52
+ rect = CGRect (x , y , 1.0 , 1.0 )
53
+ img = cg .CGWindowListCreateImage (rect ,
54
+ kCGWindowListOptionOnScreenOnly ,
55
+ kCGNullWindowID ,
56
+ kCGWindowImageDefault )
57
+ if not img :
58
+ raise RuntimeError ("Unable to capture screen image. Please ensure Screen Recording permission is granted." )
59
+
60
+ # Get the data provider from the image
61
+ provider = cg .CGImageGetDataProvider (img )
62
+ # Copy image data
63
+ cfdata = cg .CGDataProviderCopyData (provider )
64
+ # Get length of data
65
+ length = cf .CFDataGetLength (cfdata )
66
+ # Get pointer to byte data
67
+ buf = cf .CFDataGetBytePtr (cfdata )
68
+
69
+ # Default pixel format is BGRA
70
+ b , g , r , a = buf [0 ], buf [1 ], buf [2 ], buf [3 ]
71
+
72
+ # Release CoreFoundation objects to avoid memory leaks
73
+ cf .CFRelease (cfdata )
74
+ cf .CFRelease (provider )
75
+ cf .CFRelease (img )
76
+
77
+ return r , g , b , a
78
+
0 commit comments