Skip to content

Instantly share code, notes, and snippets.

@lomereiter
Created May 23, 2015 19:59
Show Gist options
  • Save lomereiter/b39eebb6118c46e9793d to your computer and use it in GitHub Desktop.
Save lomereiter/b39eebb6118c46e9793d to your computer and use it in GitHub Desktop.
automatic brightness adjustment on external monitor
import cv
import commands
import time
from PIL import Image
from collections import deque
camera = cv.CreateCameraCapture(-1)
prev_brightness_values = deque([], 5)
prev_brighness = None
def mean(collection):
return float(sum(collection)) / len(collection)
while 1:
frame = cv.QueryFrame(camera)
pi = Image.fromstring("L", cv.GetSize(frame), frame.tostring())
pi.thumbnail((32, 24, ))
image = [ord(i) for i in pi.tostring()]
brightness = mean(image) / 256.0 * 100.0
prev_brightness_values.append(brightness)
brightness = int(mean(prev_brightness_values))
if brightness != prev_brighness:
cmd = "ddccontrol -p -r 0x10 -w %d" % brightness
status, output = commands.getstatusoutput(cmd)
assert status is 0
print("set brightness to %s" % brightness)
else:
print("keeping brightness at %s" % brightness)
prev_brighness = brightness
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment