📜 ⬆️ ⬇️

Light sensor from the webcam of your laptop

image
Almost always for the night (or in the morning, with the words "What? Dawn already?") I turn on a movie or series on my laptop. To mumble and reassure. Recently I noticed that I did not reduce the brightness at all when I turned it on. Light shines in the eyes, prevents sleep. It can be solved by turning to the other side or by dimming, of course, but I am too lazy to look for Fn + to dim or set and display a brightness applet. I didn’t want to set a decrease in brightness when I was inactive, because it annoys me (you drink yourself coffee and you look at the code, it’s sharp to zero so sharply). I remembered about my old Nokia E70 (oh, and there was a smart phone), in which there was a light indicator. I thought that such a sensor can be replaced by an ordinary camera.

Nothing unusual. We photograph ourselves what the camera sees, discolor it, determine the average color and set the new brightness.
#!/bin/bash x=320 #  y=240 #  n=1000 #    ( 0  x*y) -  N      max=40 # "" ( 0  255) sleep=60 #,    maxbright=100 #  minbright=20 #  while [ 1 ]; do #  ffmpeg -f video4linux2 -s ${x}x${y} -i /dev/video0 -f image2 /tmp/snapshot.jpg 2>/dev/null #  convert /tmp/snapshot.jpg -colorspace gray /tmp/snapshot.jpg #  sum=0 count=0 let "s = x*y" #  color=(`convert /tmp/snapshot.jpg[${x}x${y}+0+0] -depth 8 txt: | tail -n +2 | sed -n 's/^.*\(#[^ ]*\).*$/\1/p' | cut -c2-3`); #  ,     R  RGB,  .    for i in `seq 0 $n $s`; #  N- do #   16-    color1=`echo ${color[i]} | cut -c1-1` color2=`echo ${color[i]} | cut -c2-2` case "$color1" in "A" ) color1=10;; "B" ) color1=11;; "C" ) color1=12;; "D" ) color1=13;; "E" ) color1=14;; "F" ) color1=15;; esac case "$color2" in "A" ) color2=10;; "B" ) color2=11;; "C" ) color2=12;; "D" ) color2=13;; "E" ) color2=14;; "F" ) color2=15;; esac let "rgbcolor = color1*16+color2" let "sum = sum+rgbcolor" # "".         let "count = count+1" #  done let "avcolor = sum/count" #  #echo ": $avcolor" #       (   max) let "bright=avcolor*100/$max" # #      if [ $bright -gt $maxbright ]; then bright=$maxbright fi if [ $bright -lt $minbright ]; then bright=$minbright fi xbacklight -set $bright #   echo " : $bright" sleep $sleep # done 

Maximum brightness to watch when there is a lot of light. I set it lower than it does so that with small changes the brightness does not jump back and forth. I hope the script will help the eyes at least somehow. In the dark it is still better to sit with a slightly damped monitor than with a brightness of 100%.
Everything is written for myself; I didn’t see anything on the Internet, so I’m sharing it.
What you need to install (for script performance): ffmpeg, ImageMagick and xbacklight.
Improvements, bugs and everything is waiting in the comments. I know that the code is not perfect.

PS: in the picture is a camera from a laptop ASUS N53sv (I have one). If you close the curtain, the brightness drops to a minimum, which confirms the work of the script. Paranoid, envy.

UPD: romik suggests replacing math with
 avcolor=`convert snapshot.jpg -colorspace GRAY -resize 1x1 txt: | sed 's/[^(]*(\s*\([0-9]*\),.*/\1/p;d'` 

A wrewolf change the brightness through acpi
 echo -n $bright > /proc/acpi/video/VGA/LCD/brightness; 

')

Source: https://habr.com/ru/post/136388/


All Articles