diff --git a/.gitignore b/.gitignore index 9f33c8d..df06075 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -lifxlan/ \ No newline at end of file +lifxlan/ +monitor-1.png diff --git a/README.md b/README.md new file mode 100644 index 0000000..88537d6 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# Screenlifx +Set LIFX color with the average screen color + +## Open +``` +python3 screenlifx.py +``` + +## Close +Ctrl + C in Python3 interpreter diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8bdd1cb --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +lifxlan==1.2.5 +mss==4.0.3 +Pillow==6.1.0 diff --git a/screenlifx.py b/screenlifx.py old mode 100644 new mode 100755 index b9995ec..369cf49 --- a/screenlifx.py +++ b/screenlifx.py @@ -1,13 +1,13 @@ -#! python2.7-32 +#!/usr/bin/env python3 # some quick & dirty code to set LIFX color with the average # screen color. Works for VLC, some games, etc. # use the LIFX LAN protocol via the excellent library by Meghan Clark # https://github.com/mclarkk/lifxlan -from lifxlan import * # +from lifxlan import * -from PIL import ImageGrab +from mss import mss from PIL import Image import time, os, colorsys @@ -17,16 +17,16 @@ def rgb2hsv(r, g, b): h = h * 0xffff s = s * 0xffff v = v * 0xffff - return h , s, v + return(h, s, v) def main(): # start with the LIFX business... - lifx = LifxLAN(1) - print "Discovering lights..." + lifx = LifxLAN(None) + print("Discovering lights...") devices = lifx.get_lights() for d in devices: - print "{} ({}) HSBK: {}".format(d.get_label(), d.mac_addr, d.get_color()) - print + print("{} ({}) HSBK: {}".format(d.get_label(), d.mac_addr, d.get_color())) + print() # the block of data to analyze. # PIL resizing to 1x1 seems to do strange things; a bigger box works better @@ -37,14 +37,23 @@ def main(): # screen scanning loop while True: - img = ImageGrab.grab() - t1 = time.clock() + with mss() as sct: image_file = sct.shot() + img_org = Image.open(image_file) + width_org, height_org = img_org.size + width = int(width_org * 0.75) + height = int(height_org * 0.75) + # best down-sizing filter + #img_resized = img_org.resize((width, height), Image.ANTIALIAS) + # quickest down-sizing filter + img_resized = img_org.resize((width, height), Image.NEAREST) + img = img_resized + t1 = time.process_time() # let PIL to shrink the image into a more manageable size # (just few ms in your average machine) img = img.resize((rx, ry)) red = green = blue = 0 - for y in xrange(0, img.size[1]): - for x in xrange(0, img.size[0]): + for y in range(0, img.size[1]): + for x in range(0, img.size[0]): c = img.getpixel((x,y)) red = red + c[0] green = green + c[1] @@ -52,10 +61,10 @@ def main(): red = red / totpixels green = green / totpixels blue = blue / totpixels - t2 = time.clock() + t2 = time.process_time() - print "\rRGB %3d %3d %3d" % (red, green, blue), - print "- Time %2.4f" % (t2-t1), + print("\rRGB {:.1f} {:.1f} {:.1f}".format(red, green, blue)) + print("- Time {}".format(t2-t1)) h, s, v = rgb2hsv(red, green, blue) color = (h, s, v, 0)