-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlight_changer.py
68 lines (51 loc) · 2.42 KB
/
light_changer.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
60
61
62
63
64
65
66
67
68
"""
A simple python program to allow for setting color of a WiFi connected light to the current color on screen.
Author: David Chen
"""
import requests
import pyscreenshot as ImageGrab
# Configs
# smaller screenshot region -> faster screenshot and faster dominant color determination
SCREENSHOT_REGION = (940, 520, 980, 560) # coordinates of top left and bottom right of rectangle: (x1, y1, x2, y2)
# enabling USE_COLORTHIEF will provide better results but will run slightly slower
USE_COLORTHIEF = True # if True, uses ColorThief to grab dominant color, otherwise just use top left pixel color
def get_color(region, colorthief=True):
""" Screenshot a portion of the screen and return the rgb tuple of the most dominant color """
im = ImageGrab.grab(bbox=SCREENSHOT_REGION, backend='mss', childprocess=False)
if colorthief: # use ColorThief module to grab dominant color from screenshot region
from colorthief import ColorThief
im.save('screenshot.png')
color_thief = ColorThief('screenshot.png')
color = color_thief.get_color(quality=1) # dominant color
else:
color = im.getpixel((0, 0)) # return color of top left pixel of region
return color
def set_light_color(color):
""" Set lifx light color to provided rgb tuple """
if sum(color) <= 30: # color is very dark, basically black
color = (0, 0, 100) # set color to blue since this is the closest to darkness
rgb = 'rgb:' + ','.join(map(str, color)) # convert (r, g, b) -> rgb:r,g,b
token = "API TOKEN HERE"
headers = {
"Authorization": "Bearer %s" % token,
}
# brightness can be set to any value from 0.0 to 1.0, or the line can be removed
# not always setting max brightness gives greater color accuracy; however, some colors can be pretty dim
payload = {
"color": rgb,
"duration": 0.4,
"brightness": 1.0,
}
response = requests.put('https://api.lifx.com/v1/lights/all/state', data=payload, headers=headers)
return response
if __name__ == '__main__':
prev_color = (0, 0, 0)
while True:
try:
color = get_color(SCREENSHOT_REGION, colorthief=USE_COLORTHIEF)
if color != prev_color:
set_light_color(color)
prev_color = color
except KeyboardInterrupt:
set_light_color((255, 255, 255)) # reset light to max brightness after stopping program
break