├── README.md └── light_changer.py /README.md: -------------------------------------------------------------------------------- 1 | ## Introduction: 2 | This is a simple python program that changes the color of my Lifx A19 bulb to the 3 | color on my screen. 4 | 5 | ## Usage: 6 | Currently this program only works for lifx bulbs and linux. However, if your lightbulb 7 | has an api, it shouldn't be too hard to configure it. I'm using pyscreenshot to take 8 | a screenshot of a single pixel to find its color, but on other OSes other python modules 9 | can be used. Finally, if the one pixel sample is too small, experimenting with ColorThief 10 | can make it easier to grab the dominant color from an image. Use Ctrl+C to stop the program. 11 | 12 | ## Demo: 13 | ![GeometryDash](https://user-images.githubusercontent.com/37674516/82279834-6f80b100-995b-11ea-9344-ab22306bae63.gif) 14 | -------------------------------------------------------------------------------- /light_changer.py: -------------------------------------------------------------------------------- 1 | """ 2 | A simple python program to allow for setting color of a WiFi connected light to the current color on screen. 3 | 4 | Author: David Chen 5 | """ 6 | 7 | 8 | import requests 9 | import pyscreenshot as ImageGrab 10 | 11 | # Configs 12 | # smaller screenshot region -> faster screenshot and faster dominant color determination 13 | SCREENSHOT_REGION = (940, 520, 980, 560) # coordinates of top left and bottom right of rectangle: (x1, y1, x2, y2) 14 | # enabling USE_COLORTHIEF will provide better results but will run slightly slower 15 | USE_COLORTHIEF = True # if True, uses ColorThief to grab dominant color, otherwise just use top left pixel color 16 | 17 | 18 | def get_color(region, colorthief=True): 19 | """ Screenshot a portion of the screen and return the rgb tuple of the most dominant color """ 20 | im = ImageGrab.grab(bbox=SCREENSHOT_REGION, backend='mss', childprocess=False) 21 | if colorthief: # use ColorThief module to grab dominant color from screenshot region 22 | from colorthief import ColorThief 23 | im.save('screenshot.png') 24 | color_thief = ColorThief('screenshot.png') 25 | color = color_thief.get_color(quality=1) # dominant color 26 | 27 | else: 28 | color = im.getpixel((0, 0)) # return color of top left pixel of region 29 | 30 | return color 31 | 32 | 33 | def set_light_color(color): 34 | """ Set lifx light color to provided rgb tuple """ 35 | if sum(color) <= 30: # color is very dark, basically black 36 | color = (0, 0, 100) # set color to blue since this is the closest to darkness 37 | 38 | rgb = 'rgb:' + ','.join(map(str, color)) # convert (r, g, b) -> rgb:r,g,b 39 | 40 | token = "API TOKEN HERE" 41 | 42 | headers = { 43 | "Authorization": "Bearer %s" % token, 44 | } 45 | 46 | # brightness can be set to any value from 0.0 to 1.0, or the line can be removed 47 | # not always setting max brightness gives greater color accuracy; however, some colors can be pretty dim 48 | payload = { 49 | "color": rgb, 50 | "duration": 0.4, 51 | "brightness": 1.0, 52 | } 53 | 54 | response = requests.put('https://api.lifx.com/v1/lights/all/state', data=payload, headers=headers) 55 | return response 56 | 57 | 58 | if __name__ == '__main__': 59 | prev_color = (0, 0, 0) 60 | while True: 61 | try: 62 | color = get_color(SCREENSHOT_REGION, colorthief=USE_COLORTHIEF) 63 | if color != prev_color: 64 | set_light_color(color) 65 | prev_color = color 66 | except KeyboardInterrupt: 67 | set_light_color((255, 255, 255)) # reset light to max brightness after stopping program 68 | break 69 | --------------------------------------------------------------------------------