journal extended

Just another WordPress.com weblog

gnome background changer

leave a comment »

see that notification
I got myself playing with a lot of scripting language lately…
so, this is a python script that change your gnome wallpaper based on given directory.
Its default setting is to change the wallpaper every 1 minute, the setting is stored in gconf
and you can change it from gconf-editor-> /desktop/gnome/background
the script will create two keys “picture_folder” and “timeout”
okay, this is it…


#!/usr/bin/env python

import gconf, os, time, pynotify
from PIL import Image

pynotify.init("BgChanger")

DEFAULTWPDIR = "/usr/share/backgrounds"
NTITLE = "Up Next..."

class BgChanger:
"""Background changer for Gnome Desktop,
scan image in specified directory and applied those images
as wallpaper via gconf."""
def __init__(self):
"init bgchanger set base directory"
    self.gcHnd = gconf.client_get_default()
    self.baseDir = DEFAULTWPDIR
    self.timeout = 1
    self.cursor = 0;
    self.res_default = [1280,1280]
    self.cacheDir = os.path.expanduser("~/.cache")
    self.notif = None
    self.startup = True
    self.images = None

def __resize(self, filename, create_thumb = False):
"image resizer, final image stored in .cache folder"
    original = filename
    img = Image.open(filename)
    if create_thumb:
        x, y = [100, 100]
    else:
        x, y = self.res_default
    x0, y0 = img.size
    if x0 > x: y0 = max(x * y0 / x0, 1); x0 = x
    if y0 > y: x0 = max(y * x0 / y0, 1); y0 = y
    if (x0,y0) == img.size: return filename
    im = img.resize([x0,y0],Image.ANTIALIAS)
    if create_thumb:
        filename = os.path.join(self.cacheDir,"thumb.png")
    else:
        filename = os.path.join(self.cacheDir, "walpaper.png")
    try:
        im.save(filename)
    except:
        filename = original
    return filename

def __scanImage(self, ext = [".jpg", ".png"]):
"directory scanner, create images list from specified directory"
    temp = [os.path.normcase(f) for f in os.listdir(self.baseDir)]
    return [os.path.join(self.baseDir, f) for f in temp
    if os.path.splitext(f)[1] in ext]

##TODO:GUI to queue image and set timer
  
def __queueImage(self, image):
"queue image file"
    self.images.append(image)
    
def __setTimer(self, timeout = 10):
    "set timeout for each background to change, timeout in minute"
    self.timeout = timeout

def __setWallpaper(self):
"change the wallpaper"
    picture_folder = self.gcHnd.get_string("/desktop/gnome/background/picture_folder")
    if picture_folder != self.baseDir:
        if picture_folder == None:
            self.gcHnd.set_string("/desktop/gnome/background/picture_folder", DEFAULTWPDIR)
            self.baseDir = DEFAULTWPDIR
        else:
            self.baseDir = picture_folder
            self.images = self.__scanImage()
        self.cursor = 0
    self.timeout = self.gcHnd.get_int("/desktop/gnome/background/timeout")
    if self.timeout == 0:
        self.gcHnd.set_int("/desktop/gnome/background/timeout", 1)
        self.timeout = 1
    if self.images == None:
        self.images = self.__scanImage()
        current = self.gcHnd.get_string("/desktop/gnome/background/picture_filename")
    if (current in self.images) and self.startup:
        self.cursor = self.images.index(current)
        self.startup = False
    else:
        self.gcHnd.set_string("/desktop/gnome/background/picture_filename", self.__resize(self.images[self.cursor]))
    self.cursor = self.cursor + 1
    if self.cursor == len(self.images): self.cursor = 0
    self.__notifyNext()

def __notifyNext(self):
"use pynotify to tell user which wallpaper will be used next"
    BODY = "%s\n%s" % (self.images[self.cursor], "in %d minute(s)" % self.timeout)
    if self.notif == None:
        self.notif = pynotify.Notification(NTITLE, BODY, self.__resize(self.images[self.cursor], True))
    else:
        self.notif.update(NTITLE, BODY, self.__resize(self.images[self.cursor], True))
    self.notif.show()

def run(self):
"setup and run the daemon"
    while 1:
    self.__setWallpaper()
    time.sleep(self.timeout * 60)

if __name__ == "__main__":
    gen = BgChanger()
    gen.run()

or fork it from the gist here:
http://gist.github.com/403027

in other note…
midori is slightly faster than firefox… but its lack of handling iframe is disturbing

Advertisement

Written by fudanchii

May 16, 2010 at 5:26 pm

Posted in script

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.