Random Wallpaper Rotation in Gnome

I wanted to randomly switch the wallpaper in my desktop in Gnome, but didn’t find an obvious or intuitive way to do it, so I created my own script. Here are two revisions of the script, bgotd.pl and bgotd-more.pl. bgotd of course stands for BackGround Of The Day, even though I rotate my image every 5 minutes.

This page is OLD and may contains errors, out of date information, security flaws or other problems.

In truth, my final solution was slightly more complex than either of these. I actually scp myfile to a web server, so that all of our computers can then download the image every now and then. These should fit more users needs though, and anyone who needs to scp their files will know how to modify these scripts themselves. My script also uses cron, rather than acting like a daemon.

bgotd.pl

Stick this somewhere, make it executable and put it in your Gnome session.

#!/usr/bin/perl -w
use strict;
use warnings;

### Start CONFIG

my $searchPath = '/path/to/your/top/level/photo/directory/';
my $switchTime = 300;                 # Time is in seconds

### END CONFIG

# bgotd-- background of the day
#
# A super simple script which randomly picks a jpg/JPG/jpeg/JPEG photo to place as your
# Gnome desktop.
#
# Written by Michael Moore, Nov. 2007
# This work is hereby placed in the public domain 

my @photos = `find $searchPath -type f | grep [jJ][pP][eE]*[gG]`;
chomp(@photos);
my $photo;

while(1)
{
    $photo = $photos[rand($#photos)];
    `gconftool-2 --type string --set /desktop/gnome/background/picture_filename "$photo"`;
    sleep($switchTime);
}

bgotd-more.pl

This requires the perl module Image::Magick as well as a command line imagemagick installation. It works better on wide screen monitors than the other one does though.

#!/usr/bin/perl -w
use strict;
use warnings;
use Image::Magick;

### Start CONFIG

my $searchPath = '/path/to/your/photo/directory/';
my $switchTime = 600;            # time in seconds
my $maxDimensions = '1440x900';  # What's the max size of the photo.
                                 # They will be resized to this dimension
				 # if needed (lets two different sized
				 # pictures be placed side by side without funny blank space.

### END CONFIG

# bgotd-more -- background of the day (more)
#
# A pretty simple script which randomly picks a jpg/JPG/jpeg/JPEG photo to place as your
# Gnome desktop. If the photo is taller than it is wide, it finds another
# taller-than-wide photo and puts them side by side with Imagemagick
#
# Written by Michael Moore, Nov. 2007
# This work is hereby placed in the public domain 

my $photoa = "";
my $photob = "";
my @photos = `find $searchPath -type f | grep [jJ][pP][eE]*[gG]`;
chomp(@photos);

while(1)
{
    $photoa = $photos[rand($#photos)];
    my $pa = new Image::Magick;
    $pa->Read($photoa);
    my ($ha,$wa) = $pa->Get('height','width');

    if($ha > $wa){

	`cp "$photoa" /tmp/bgotda.jpg`;
	`mogrify -resize $maxDimensions /tmp/bgotda.jpg`;

	my $hb = 0;
	my $wb = 1;
	while($hb < $wb){
	    $photob = $photos[rand($#photos)];
	    my $pb = new Image::Magick;
	    $pb->Read($photob);
	    ($hb,$wb) = $pb->Get('height','width');
	}

	`cp "$photob" /tmp/bgotdb.jpg`;
	`mogrify -resize $maxDimensions /tmp/bgotdb.jpg`;

	`convert /tmp/bgotda.jpg /tmp/bgotdb.jpg +append /tmp/bgotd.jpg`;
    }
    else
    {
	`cp "$photoa" /tmp/bgotd.jpg`;
    }

    `mogrify -resize $maxDimensions /tmp/bgotd.jpg`;

    `cp /tmp/bgotd.jpg ~/.bgotd.jpg`;
    `gconftool-2 --type string --set /desktop/gnome/background/picture_filename ~/.bgotd.jpg`;

    sleep($switchTime);
}

This entry was posted in Programming, Projects and tagged , , , , . Bookmark the permalink.

One Response to Random Wallpaper Rotation in Gnome

  1. Duncan says:

    That’s great – so easy and fast!
    Thanks a lot

Leave a Reply

Your email address will not be published. Required fields are marked *