You are not logged in.

#1 2012-05-04 17:07:56

stqn
Member
Registered: 2010-03-19
Posts: 1,191
Website

Randomly select a wallpaper based on desired brightness [Xfce]

Here’s a small bash script I wrote to have random, more or less bright, wallpapers, depending on my redshift setting. This way I can automatically have dark wallpapers at night and bright wallpapers in the day. The wallpapers are changed after a chosen delay.

There are actually two scripts. The first one sets redshift to my desired values, and then calls the second one that changes the wallpaper.

redshift.sh:

#!/bin/bash

if [[ $# != 1 ]]; then
	echo "Usage: $0 <percentage> (0 = dark, 100 = bright)"
	exit
fi

min_color=3900
max_color=5550
min_light=0.65
max_light=1.0

# Update current value displayed in the menu
#sed -i "s/Current: [^%]*%/Current: $1%/" "$XDG_DATA_HOME/redshift.sh/redshift-display.desktop"

# Apply exponential function to percentage to make the progression more linear
# to the eye
#pct=$(echo "scale=2;100*(e($1/100)-1)/(e(1)-1)"|bc -l)
pct=$(echo $1 | awk '{printf "%d", (100*(2^($1/100)-1)/(2^1-1)+0.5)}')

color=$(echo "$min_color+($max_color-$min_color)*$pct/100" | bc)
light=$(echo "scale=2;$min_light+($max_light-$min_light)*$pct/100" | bc)

pkill '^redshift$'
nohup redshift -l 45.0:5.0 -t $color:$color -b $light:$light -r &>/dev/null &

# Update background picture
pkill 'darkpaper.sh'
lum=$(($1 * 85 / 100 + 5))
nohup ./darkpaper.sh $lum &>/dev/null &

darkpaper.sh:

#!/bin/bash

d=~/images/wallpapers
delay=1800

if [[ $# != 1 ]]; then
	echo "Usage: $0 <percentage> (0 = dark, 100 = bright)"
	exit
fi

pct=$(echo $1 | awk '{printf "%d", (100*(2^($1/100)-1)/(2^1-1)+0.5)}')
pct_min=$(echo $(($1-10)) | awk '{printf "%d", (100*(2^($1/100)-1)/(2^1-1)+0.5)}')
pct_max=$(echo $(($1+20)) | awk '{printf "%d", (100*(2^($1/100)-1)/(2^1-1)+0.5)}')

wanted_brightness=$((pct*255/100))
min_brightness=$((pct_min*255/100))
max_brightness=$((pct_max*255/100))

cd $d
list=($(find . -maxdepth 1 -type f | shuf))
index=0

while true; do

	pic=${list[$index]}

	tmp=$(identify -verbose "$pic" | grep -A6 -E "Overall:|Gray:")
	mean=$(echo "$tmp" | grep "mean:" | awk '{print $2}' | cut -d'.' -f1)
	stddev=$(echo "$tmp" | grep "deviation:" | awk '{print $3}' | cut -d'.' -f1)
	pic_brightness=$((mean+stddev))

	if ((pic_brightness >= min_brightness && pic_brightness <= max_brightness)); then
		if ((pic_brightness >= wanted_brightness)); then
			delta_brightness=$((wanted_brightness - pic_brightness))
		else
			delta_brightness=0
		fi

		xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/brightness -s $delta_brightness
		xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s $d/$pic

		sleep $delay
	fi

	index=$((index+1))
	if (( index == ${#list[*]} )); then
		index=0
	fi
done

Ok, many notes…

redshift.sh:
- requires redshift, obviously smile
- you can change min/max_color and min/max_light to your liking.
- the commented-out “sed” line… is because I’m using a custom applications menu to launch this script from Xfce4’s panel. I’ll explain how to do it if someone asks.

darkpaper.sh:
- requires imagemagick for its “identify” tool that is used to get the pictures’ brightness.
- doesn’t work with filenames containing spaces.
- might loop forever if there is no adequate picture in your collection.
- you need to set “d” to your wallpapers’ directory.
- you can set “delay” to the time in seconds before a new picture is set.
- you can remove “-maxdepth 1” if you want to recursively scan your pictures’ directory.
- you might want to try changing “pic_brightness=$((mean+stddev))” to simply “pic_brightness=$mean” and see if that gives better results.
- the script also changes the brightness setting of xfce4-desktop if the picture is a bit too bright (but not if it is a bit too dark.)

Enjoy wink

Latest edit: removed useless “echo $index” from darkpaper.sh.

Last edited by stqn (2012-05-04 20:27:46)

Offline

#2 2012-05-04 20:12:16

daneel971
Member
Registered: 2008-03-28
Posts: 197

Re: Randomly select a wallpaper based on desired brightness [Xfce]

Many thanks for sharing this.

Offline

Board footer

Powered by FluxBB