Wednesday, November 26, 2008

Changing your wallpaper randomly

I seem to have a sizable wallpaper collection which makes it hard to choose what wallpaper i wish to use currently. Thankfully, there's a plethora of tools & documentation to help me resolve this little dilemma of mine. So, I wrote a little Python script to randomly pick a wallpaper everytime it's run, then set that as my current desktop wallpaper.

Here's the script:

#!/usr/bin/python

import gconf
import os
import random
import sys
import mimetypes

bg = '/path/to/folder'
key = '/desktop/gnome/background/picture_filename'
backgrounds = []

for root, dirs, files in os.walk(bg):
for f in files:
mimetype = mimetypes.guess_type(f)[0]
if mimetype and mimetype.split('/')[0] == 'image':
backgrounds.append(os.path.join(root, f))

client = gconf.client_get_default()
client.unset(key)
client.set_string(key, random.choice(backgrounds))

Just assign bg to a string that points to the folder where all your wallpaper are :-P