<-- Back to Andy Balaam Home

Random Background

Random Background is a little script to show a different photo each day on your GNOME desktop background.

Prerequisites

You will need python, and if you have exiftran it will be used to make sure the photos are properly rotated.

On Ubuntu just type:

 sudo apt-get install python exiftran

Download

Download random_background (right-click the link and choose "Save As") and save it somewhere e.g. /home/user/bin/random_background .

NOTE: above and elsewhere you should replace "user" with your username.

Install

To change your background immediately, you can simply run the script from the command line like this:

 /home/user/bin/random_background ~/Photos

(Assuming there are some photos (in .jpg format) in the Photos directory inside your home directory.

To make your background change every day, you need to add a line to your crontab. Start editing your crontab by typing this:

 crontab -e

and in the editor that appears, add a line like this:

 0  6  *  *  *  /home/user/bin/random_background /home/user/Photos

This will change your desktop background at 6am every day.

Code

 #!/usr/bin/python

 import os
 import os.path
 import random
 import sys

 if len( sys.argv ) != 2:
 	sys.exit( "You must supply a directory which contains .jpg photos." )

 photos_dir = sys.argv[1]

 random.seed()

 names = []

 for (dirpath, dirnames, filenames) in os.walk( photos_dir ):
     for fn in filenames:
         if fn[-4:].lower() == ".jpg":
             names.append( dirpath + "/" + fn )

 if len( names ) == 0:
 	sys.exit( "No .jpg photos were found in ' photos_dir )

 chosen_fn = names[ random.randint( 0, len( names ) ) ]

 if os.path.isfile( '/usr/bin/exiftran' ):
 	bgs_dir = os.path.join( os.path.expanduser( "~" ), ".random_background" )
 	if not os.path.isdir( bgs_dir ):
 		os.mkdir( bgs_dir )
 	bg_file = os.path.join( bgs_dir, "bg.jpg" )
 	os.system( '/usr/bin/exiftran -a -o "s"' % ( bg_file, chosen_fn ) )
 	chosen_fn = bg_file

 os.system( '/usr/bin/gconftool-2 --type=string '
 	'--set /desktop/gnome/background/picture_filename " chosen_fn )

Contact

Andy Balaam <axis3x3 at users dot sourceforge dot net>

Return to Andy Balaam's home page

Edit | History | Print | Recent Changes | Search | Admin Page last modified on October 28, 2009, at 05:29 PM