Random Wallpaper Rotation in Gnome

Posted by stuporglue on Mar 14, 2010 in Programming, Projects
Subscribe to posts by category...
Programming, Projects

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);
}
Tell Your Friends
  • Digg
  • Facebook
  • Google Bookmarks
  • RSS
  • StumbleUpon
  • Twitter

Tags: , , , ,

Leave a Reply

XHTML: You can use these tags:' <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Original content on this page is under the Creative Commons Attribution-ShareAlike 3.0 License unless otherwise noted.

If you found this page particularly helpful, I'd love to hear from you. A link to this page from your blog or website would also be appreciated.

I'm the real Michael Moore