Created
May 16, 2010 17:39
-
-
Save fudanchii/403027 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment