Skip to content

Instantly share code, notes, and snippets.

@error454
Created May 22, 2016 22:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save error454/3ef46c07ece028115fcb030531f5bd4c to your computer and use it in GitHub Desktop.
Save error454/3ef46c07ece028115fcb030531f5bd4c to your computer and use it in GitHub Desktop.
Ambilight emulation using python
from PIL import ImageGrab
from PIL import ImageStat
from phue import Bridge
from time import sleep
import time
import math
import zachhue
import colorsys
width = 1920
height = 1080
boundingbox = (int(width * 0.4), int(height * 0.4), int(width * 0.8), int(height * 0.8))
resize = True
destWidth = 20
lastXY = 0
lastBrightness = 0
minBrightness = 100
maxBrightness = 255
while 1:
# fullscreen
im = 0
if(resize):
im=ImageGrab.grab()
im = im.resize((destWidth, int(destWidth / (width/height))))
else:
im=ImageGrab.grab()
qstats = im.quantize(1).convert("RGB").getpixel((0, 0))
xy = zachhue.RGBtoXY(qstats[0], qstats[1], qstats[2])
#hls = colorsys.rgb_to_hsv(stats.mean[0], stats.mean[1], stats.mean[2])
#brightness = int(math.fabs(math.sin(time.clock() * 0.5)) * (255-60)) + 60
#print(brightness)
if lastXY == xy:
newBrightness = 0
if lastBrightness == minBrightness:
newBrightness = maxBrightness
else:
newBrightness = minBrightness
zachhue.OnXY(zachhue.office, xy, newBrightness, 10)
lastBrightness = newBrightness
sleep(3)
else:
zachhue.OnXY(zachhue.office, xy, lastBrightness, 0.5)
lastXY = xy
#sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment