This repository has been archived by the owner on Sep 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
59 lines (47 loc) · 1.69 KB
/
main.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import time
import numpy as np
import screen_brightness_control as sbc
from PIL import ImageGrab
from screeninfo import get_monitors
monitor_num = 0
for monitor in sbc.list_monitors():
print(monitor, ':', sbc.get_brightness(display=monitor), '%')
def most_frequent(a):
counter = 0
num = a[0]
for i in a:
curr_frequency = a.count(i)
if curr_frequency > counter:
counter = curr_frequency
num = i
return num
monitor_x = get_monitors()[monitor_num].x
monitor_y = get_monitors()[monitor_num].y
monitor_w = get_monitors()[monitor_num].width
monitor_h = get_monitors()[monitor_num].height
print(monitor_x, monitor_y, monitor_w, monitor_h)
step = 50
num_pixel = monitor_w // step * monitor_h // step
count = 0
while True:
try:
img = ImageGrab.grab()
imgNP = np.array(img)
im_arr = np.frombuffer(img.tobytes(), dtype=np.uint8)
im_arr = im_arr.reshape((img.size[1], img.size[0], 3))
r = g = b = 0
pixelArray = []
for y in range(monitor_y, monitor_h, step):
for x in range(monitor_x, monitor_w, step):
px = im_arr[y][x]
pixelArray.append([px[0], px[1], px[2]])
mostFrequentColor = most_frequent(pixelArray)
count += 1
e = round(
(((int(mostFrequentColor[0]) + int(mostFrequentColor[1]) + int(mostFrequentColor[2])) / 3) / 255) * 50)
target = e + 50
sbc.fade_brightness(target, display=sbc.list_monitors()[monitor_num])
except:
# Quick fix for when the device is asleep
print("Error, (fell asleep?)")
time.sleep(0.2) # print(count)