Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
lifxlan/
lifxlan/
monitor-1.png
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Screenlifx
Set LIFX color with the average screen color

## Open
```
python3 screenlifx.py
```

## Close
Ctrl + C in Python3 interpreter
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lifxlan==1.2.5
mss==4.0.3
Pillow==6.1.0
39 changes: 24 additions & 15 deletions screenlifx.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -37,25 +37,34 @@ 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]
blue = blue + c[2]
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)
Expand Down