Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there a way to have my webcam sense lighting conditions and adjust screen brightness accordingly?

share|improve this question

3 Answers 3

up vote 8 down vote accepted

I've improved on my previous attempt:

import opencv
import opencv.highgui
import time
import commands

def get_image():
    image = opencv.highgui.cvQueryFrame(camera)
    return opencv.adaptors.Ipl2PIL(image)

camera = opencv.highgui.cvCreateCameraCapture(-1)

while 1:
    image = get_image()
    image.thumbnail((32, 24, ))
    image = tuple(ord(i) for i in image.tostring())
    x = int((int((max(image) / 256.0) * 10) + 1) ** 0.5 / 3 * 10)
    cmd = ("sudo su -c 'echo " + str(x) +
        " > /sys/devices/virtual/backlight/acpi_video0/brightness'")
    status, output = commands.getstatusoutput(cmd)
    assert status is 0

Two problems with this approach: The display brightness, at least with my webcam, never goes lower than four, because the camera doesn't work well in darkness, and it might skip a bit between four and five. Anyway,

This is how to use it:

  1. sudo apt-get install python-opencv
  2. Save this script somewhere, say ~/test.py
  3. Run it via python test.py

Instead of the average brightness, I'm now getting the maximum brightness of a small tumbnail (this is to avoid dead pixel problems and make it faster). And at least with my combination of lights and camera, it works rather well!

Give it a try (:

share|improve this answer
Note that this is a very simplistic approach. It'd be very easy to get a weighted black and white histogram of the image instead of the arithmetic mean of the pixels (red and blue don't have the same brightness). Also, you could set a minimum, maximum and have it switch only after a certain level of confidence that the ambient light has changed. But I didn't bother because of the issues I mentioned. It would have the same problems as this one. –  Stefano Palazzo Jan 28 '11 at 1:05
Hmm, well it works like you say. :) –  zpletan Jan 28 '11 at 14:45
@zpletan give it another go; I've started from scratch. –  Stefano Palazzo Feb 2 '11 at 16:23
Shouldn't that normalization be "max(image) / 255.0"? ;-) –  htorque Feb 2 '11 at 20:18
This seems to work about the way I want. Thank you! (And sorry for taking so long to answer.) –  zpletan Jun 1 '11 at 1:39

Take a look into the RedShift project, which changes the screen temperature based on your geographic location as F.Lux was formerly doing.


Instructions on installing and how to use are well documented in the website and, depending on your hardware and monitor you can feel your eyes like "refreshed" by the nice effect which is achieved by RedShift.

share|improve this answer
I don't think this answers the question, maybe you should delete your answer and make it a comment on the original question instead. Answers should always directly answer the question. –  Stefano Palazzo Jan 28 '11 at 1:57
your link does not work. –  TheX Jan 28 '11 at 2:01
It sounds like this does not take your surroundings into account? So if I was in a dark room in the middle of the day, it wouldn't do anything? –  zpletan Jan 28 '11 at 3:03
@zpletan Yes, this one only changes the colour, according to the position of the sun. –  Stefano Palazzo Jan 28 '11 at 15:51
This side the link seems to work without problems. Stefano Palazzo, sorry if you didn't find my answer useful. However, I think somebody else could find it is useful if you keep the answer. Your choice. You can vote to request deletion. :) Thank you. –  Geppettvs D'Constanzo Jan 29 '11 at 19:32

I also made a C tool for that, using OpenCV. You can find it at https://launchpad.net/brightness (you have to compile it by your own). I hope it will help you.

share|improve this answer
I've got it compiled, but how do I use it? –  zpletan Jun 1 '11 at 1:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.