-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenpixel.py
More file actions
37 lines (25 loc) · 1011 Bytes
/
screenpixel.py
File metadata and controls
37 lines (25 loc) · 1011 Bytes
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
import Quartz.CoreGraphics as CG
import struct
class ScreenPixel(object):
def __init__(self):
self._data = None
def capture(self, x, y):
region = CG.CGRectMake(x, y, 1, 1)
# Create screenshot as CGImage
image = CG.CGWindowListCreateImage(
region,
CG.kCGWindowListOptionOnScreenOnly,
CG.kCGNullWindowID,
CG.kCGWindowImageDefault)
# Intermediate step, get pixel data as CGDataProvider
prov = CG.CGImageGetDataProvider(image)
# Copy data out of CGDataProvider, becomes string of bytes
self._data = CG.CGDataProviderCopyData(prov)
def pixel(self):
# Pixel data is unsigned char (8bit unsigned integer),
# and there are four (blue,green,red,alpha)
data_format = "BBBB"
# Unpack data from string into Python'y integers
b, g, r, a = struct.unpack_from(data_format, self._data, offset=0)
# Return BGRA as RGBA
return (r, g, b)