vrijdag 22 juli 2011

Dynamic Desktop Background

Some Linux distributions have the option to refresh your desktop background wallpaper with a random image from a list of images or a specified directory. Ubuntu Natty (11.04) does not offer that option, but even if it did, I have some extra requirements:

  • Load the image recursively from a tree of directories

  • Skip portrait orientation images (these will not show nicely on you wide screen)

  • Skip small images (thumbnails)


So I wrote my own script to dust of my (bash) scripting skills:
RandomBG

It requires the installation of ImageMagick and jhead from your distribution. These are needed for determining the orientation of the image.
Store the script on your computer ($HOME/bin is a good place) and invoke it with two arguments, the image directory (tree) and the delay in seconds between refreshments, e.g.:

RandomBG /data/MyImageCollection 300

You can start the script when you login on your desktop by adding it to the list of startup programs. On Ubuntu Natty you can find this setting as Startup Applications under System Settings.

The default minimum size of an image is 200k. You can easily change it in the script on line 44 by adapting the find command.
Adapting it for KDE or XFCE would require just replacing the single gconftool command.

Update for Ubuntu 11.10 Oneiric Ocelot Unity desktop:

replace the gconftool line with

dconf write /org/gnome/desktop/background/picture-uri "'file:///$foto'"





#!/bin/bash

DIR=${1:?Need an image dir for this arg}
SLEEP=${2:-300}

function Ori()
{
FILE="$1"
ORI=`jhead -v "$FILE"|grep "Orientation ="`
set `identify -format "%w %h" "$FILE"`
W=$1
H=$2

if [[ "$ORI" == "" || "$ORI" =~ 256 ]]
then
if (( $W > $H ))
then
echo "l"
else
echo "p"
fi
else
if [[ "$ORI" =~ 1 ]]
then
if (( $W > $H ))
then
echo "l"
else
echo "p"
fi
else
if [[ "$ORI" =~ 6 ]]
then
echo "p"
else
echo "-p"
fi
fi
fi
}

OLDIFS=$IFS
IFS=$'\n'
fotos=( $(find $DIR \( -name '*.jpg' -o -name '*.JPG' \) -size +200k) )
IFS=$OLDIFS

while :
do
ori="x"
while test $ori != "l";
do
foto="${fotos[ $RANDOM % ${#fotos[@]} ]}"
ori=$(Ori "$foto");
done
echo $foto
gconftool -t string -s /desktop/gnome/background/picture_filename "$foto"
sleep $SLEEP
done

Geen opmerkingen:

Een reactie plaatsen