Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env python
import gconf, os, time, pynotify
from PIL import Image
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 =
if create_thumb:
x, y = [100, 100]
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")
filename = os.path.join(self.cacheDir, "walpaper.png")
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"
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
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
self.cursor = self.cursor + 1
if self.cursor == len(self.images): self.cursor = 0
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))
self.notif.update(NTITLE, BODY, self.__resize(self.images[self.cursor], True))
def run(self):
"setup and run the daemon"
while 1:
time.sleep(self.timeout * 60)
if __name__ == "__main__":
gen = BgChanger()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment