Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to use cron and this script (http://askubuntu.com/questions/23593/use-webcam-to-sense-lighting-condition-and-adjust-screen-brightness):

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

crontab: /30*** sudo python /home/username/screen.py

However, there are two issues:

First, can the while-loop be terminated after 5 sec or so? Second, could anyone please try to improve the script so that lower brightness-levels can be set using the script? Maybe the way the 256 brightness-levels are mapped to the ones that can be set for the screen would need to be changed.

Thanks

share|improve this question
    
Not a solution, but an alternative: have you considered f.lux? stereopsis.com/flux –  jasonwryan Sep 4 '11 at 18:12
    
Yeah, I tried redshift and f.lux, both didn't work correctly. And if I use the webcam the result should be more accurate, even if the color temperature doesn't get adjusted –  Toxicbits Sep 4 '11 at 18:41
add comment

1 Answer 1

up vote 4 down vote accepted

In older kernels there was brightness control file somewhere in /proc, but I think that it was the same functionality as /sys in your code snippet. In this /procfile there was several levels of brightness that you could use and probably the same is in that mechanism. Try to cat /sys/devices/virtual/backlight/acpi_video0/brightness and check if there is information about brightness levels that you may use.

If you remove loop whole code will be executed once so it will be possible to set brightness only once per half hour period. There is little disadvantage of this solution - if you cover camera at the moment of brightness checking, brightness will be changed and next reading will be after half hour so you end up with completely dark screen for half hour.

To avoid this situation you could replace while loop by for loop (0 to 5 for example) with 5 seconds sleep with it. In loop you can only check brightness five times and after loop you could calculate average brightness and set it.

EDIT: Code with average from 25 seconds:

import opencv
import opencv.highgui
import time
import commands
from time import sleep

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

camera = opencv.highgui.cvCreateCameraCapture(-1)

x = []

for i in range(5):
    image = get_image()
    image.thumbnail((32, 24, ))
    image = tuple(ord(i) for i in image.tostring())
    x.append(int((int((max(image) / 256.0) * 10) + 1) ** 0.5 / 3 * 10))
    sleep(5)

sum = 0
for i in x:
    sum = sum + i

avg = sum / len(x)
cmd = ("sudo su -c 'echo " + str(avg) + " > /sys/devices/virtual/backlight/acpi_video0/brightness'")
status, output = commands.getstatusoutput(cmd)
assert status is 0

Unfortunately I don't have option to change backlight (old kernel or something missing in kernel) and I don't have camera to check if it is working...

share|improve this answer
    
The output is 5. Unfortunately I don't know how to code in python, could you please try to implement that? Thx –  Toxicbits Sep 4 '11 at 18:45
    
If there is only one value I think that may be current value. In /proc version there was current value and a list of possible values. I edited my answer, but you must check on your own if it makes sense... –  pbm Sep 4 '11 at 19:02
    
Thanks a lot, the script works great :)! I use a laptop, so covering the webcam by accident won't really be an issue. I counted the brightness levels I can adjust the screen to using the Fn keys and got 11 levels. But does that matter at all? Or would this line x.append(int((int((max(image) / 256.0) * 10) + 1) ** 0.5 / 3 * 10)) need to be altered? –  Toxicbits Sep 4 '11 at 21:47
    
Could you check if there is a change in this file when you change brightness using fn? If yes whats max and min values? What is the step? –  pbm Sep 5 '11 at 6:18
    
Yes, the output is: min = 0, max = 10 and the step is 1 –  Toxicbits Sep 5 '11 at 9:08
show 3 more comments

Your Answer

 
discard

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.