Random desktop background in Gnome with python
I love my wallpapers and I keep a whole collection of them in a Dropbox folder so they sync between all my computers. The number of wallpapers grows as I encounter new ones I like but fortunately OSX has the nice feature that will show a random image one the desktop. Unfortunately however my Ubuntu install running Gnome does not have this feature.
While looking at ways how I could change my desktop background randomly in Gnome like in OSX I found out that the Gnome configuration manager (GConf) has a python api. So I spend two hours on saterday morning writing a little python script that changes my desktop background just like in OSX (with the same files provided by Dropbox).
The script is very basic but does exactly what it is expected to do. It finds images in a directory and selects one randomly to set as the background image. This is done through the GConf api that is part of Gnome. By executing this script through cron you can adjust how often the desktop background will change.
#!/usr/bin/env python
# This script will change the desktop background to a
# random image taken from a directory
#
# Use random_background.py --help for options
#
# Author: Mattijs Hoitink
# License: none (public domain)
import sys, argparse, os, mimetypes, re, random, gconf
# Simple argument parsing
parser = argparse.ArgumentParser(description="Change Gnome desktop background to a random image from a directory")
parser.add_argument('--no-recurse', help='If files should not be searched recursively', dest='recursive', action='store_false', required=False)
parser.add_argument('path', action='store', help='Path to the directory to select a wallpaper from')
args = parser.parse_args()
# Check if the path exists
abs_path = os.path.abspath(args.path)
if not os.path.exists(abs_path):
print "Path '%s' does not exist" % abs_path
sys.exit(1)
# Find files in a directory recursively (or not)
def find_files(root, recursive = True):
found = []
for root, dirs, files in os.walk(root):
# Append files that are images
for file in files:
# Get the file path and mimetype
filePath = os.path.join(root, file)
fileMimetype = mimetypes.guess_type(filePath)[0]
# Check if the found file is an image
imageRegex = re.compile(r'image/[\w\-\.]*', re.IGNORECASE)
if fileMimetype and imageRegex.match(fileMimetype):
found.append(filePath)
# Check if we need to recurse intor sub-directories
if recursive:
for dir in dirs:
found += find_files(os.path.join(root, dir), recursive)
return found
# Fetch all wallpapers from the directory
backgrounds = find_files(abs_path, args.recursive)
if 0 >= len(backgrounds):
print "No background images were found"
sys.exit(0)
# Modify the background using GConf
gconf_background_path = '/desktop/gnome/background/picture_filename'
client = gconf.client_get_default()
new_background = backgrounds[random.randint(0, len(backgrounds) - 1)]
client.set_string(gconf_background_path, new_background)
Since I only use one monitor I have no clue what the script does on multiple monitors. But I assume you just have to use desktop images that span both monitors. The script is free to use and modify.