""" Adapted from James Clarke's 2005 wallpaper example. Run this program, enter in tags you want to search for then open the wallpaper.jpg file created in the same folder as your code Requires: - Python Imaging Library [http://www.pythonware.com/products/pil/] """ __author__ = "James Clarke " __version__ = "$Rev$" __date__ = "$Date$" __copyright__ = "Copyright 2004-5 James Clarke" import sys import urllib import math import Image import flickr import random def photos_required(screen, size): """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 get_urls_for_tags(tags, number): print("Finding photos...") photos = flickr.photos_search(tags=tags, per_page=number) print("Photos: ") urls = [] for photo in photos: print "\t" + photo.title + " by " + photo.owner.username + " in " + photo.owner.location urls.append(photo.getURL(size='Square', urlType='source')) return urls def create_wallpaper(screen, urls, size, randomise): 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 range(height): y = size[1] * i for j in range(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): print("Loading picture from " + url) file, mime = urllib.urlretrieve(url) photo = Image.open(file) return photo width = 300 height = 300 file = 'wallpaper.jpg' tags = raw_input("What image tag(s) do you want your background to use? ") tags_list = tags.split() screen = (width, height) size = (75, 75) n = photos_required(screen, size) urls = get_urls_for_tags(tags_list, n) wallpaper = create_wallpaper(screen, urls, size, True) wallpaper.save(file) print("Saved in file: " + file)