-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallpaper.py
executable file
·118 lines (96 loc) · 3.09 KB
/
wallpaper.py
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
#!/usr/bin/python
"""Usage: python wallpaper.py [OPTIONS] TAGS
TAGS is a space delimited list of tags
OPTIONS:
-w screenwidth or --width screenwidth
-h screenheight or --height screenheight
-f filename or --file filename
Requires:
- Python Imaging Library [http://www.pythonware.com/products/pil/]
"""
__author__ = "James Clarke <james@jamesclarke.info>"
__version__ = "$Rev$"
__date__ = "$Date$"
__copyright__ = "Copyright 2004-5 James Clarke"
import sys
import urllib
import math
import random
import Image
import flickr
#this is slow as so many API calls
def get_urls_for_tags(tags, number):
photos = flickr.photos_search(tags=tags, per_page=number)
urls = []
for photo in photos:
urls.append(photo.getURL(size='Square', urlType='source'))
return urls
#quicker to just 'hack' the url
#I don't think this works anymore, a change in url format
def quick_get_urls_for_tags(tags, number):
photos = flickr.photos_search(tags=tags, per_page=number)
urls = []
for photo in photos:
urls.append('http://photos%s.flickr.com/%s_%s_s.jpg' %\
(photo.server, photo.id, photo.secret))
return urls
def photos_required(screen, size=(100, 100)):
"""screen is tuple (width, height)"""
width = int(math.ceil(float(screen[0]) / size[0]))
height = int(math.ceil(float(screen[1]) / size[1]))
return width * height
def create_wallpaper(screen, urls, size=(100, 100), randomise=False):
if randomise:
random.shuffle(urls)
wallpaper = Image.new("RGB", screen, "blue")
width = int(math.ceil(float(screen[0]) / size[0]))
height = int(math.ceil(float(screen[1]) / size[1]))
offset = [0,0]
for i in xrange(height):
y = size[1] * i
for j in xrange(width):
x = size[0] * j
photo = load_photo(urls.pop())
if photo.size != size:
photo = photo.resize(size, Image.BICUBIC)
wallpaper.paste(photo, (x, y))
del photo
return wallpaper
def load_photo(url):
file, mime = urllib.urlretrieve(url)
photo = Image.open(file)
return photo
def main(*argv):
from getopt import getopt, GetoptError
try:
(opts, args) = getopt(argv[1:], 'w:h:f', ['width', 'height', 'file'])
except GetoptError, e:
print e
print __doc__
return 1
width = 1024
height = 768
file = 'wallpaper.jpg'
for o, a in opts:
if o in ('-w', '--width'):
width = a
elif o in ('-h', '--height'):
height = a
elif o in ('-f' '--file'):
file = a
else:
print "Unknown argument: %s" % o
print __doc__
return 1
if len(args) == 0:
print "You must specify at least one tag"
print __doc__
return 1
tags = [item for item in args]
screen = (width, height)
n = photos_required(screen)
urls = get_urls_for_tags(tags, n)
wallpaper = create_wallpaper(screen, urls, randomise=True)
wallpaper.save(file)
if __name__ == '__main__':
sys.exit(main(*sys.argv))