Gnome Background Changer

| Comments

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…

(bgchanger.py) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python

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

pynotify.init("BgChanger")

DEFAULTWPDIR = "/home/adie/Pictures/kin iro mosaic"
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 = [1920,1920]
        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():
    "setup and run the daemon"
    cpid = -1
    try:
        cpid = os.fork()
    except:
        print "can't run as daemon"
        os.abort()
    if cpid < 0:
        print "fork failed..."
        os._exit(0)
    if cpid == 0:
        try:
            cpid = os.fork()
        except:
            print "second daemon fork failed"
            os.abort()
        if cpid < 0:
            print "second fork failed..."
            os._exit(0)
        if cpid == 0:
            return True
        print "second child now is the daemon, run at background as %d" % cpid
        os._exit(0)
    print "first child run at background as %d" % cpid
    os._exit(0)

if __name__ == "__main__":
    run()
    gen = BgChanger()
    while True:
        gen.setWallpaper()
        time.sleep(gen.timeout * 60)

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