| #! /usr/bin/env python # Copyright 2009 by Benjamin Fogle # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import sys import os import os.path import urllib2 import tempfile import math # Constants. Change these to customize the script # In ksh: CONFIG_FILE=$HOME/lectures/examples/LEC-05/webcam_config CONFIG_FILE=os.getenv("HOME") + "/.webcam_config" TARGET=os.getenv("HOME") + "/Pictures/Wallpapers/webcam.jpg" SCREEN_X = 1024 SCREEN_Y = 768 def GetTiles(num_images): """Returns a tuple (N,M) representing the tile grid""" # This algorithm could be much improved tile = math.ceil(math.sqrt(N)) return (tile, tile) def GetImageSize(N, M): """Returns a the max size of each image based on the screen size and the tile grid""" global SCREEN_X global SCREEN_Y ImgX = SCREEN_X / N ImgY = SCREEN_Y / M return (ImgX, ImgY) def CopyFile(src_fp, dest_fp): """Copy a file intelligently""" data = src_fp.read(49152) # Copy 48k at a time while data: dest_fp.write(data) data = src_fp.read(49152) # Note: shutils.copyfileobj does this too. if len(sys.argv) != 1: print "Usage: %s" % (sys.argv[0]) print "Config file is located in %s" % CONFIG_FILE sys.exit(1) # Make sure the config file is readable and that it is a regular file # There is a better way to do this that we will learn later. if not os.access(CONFIG_FILE, os.R_OK) or not os.path.isfile(CONFIG_FILE): print "Error: %s could not be opened!" % CONFIG_FILE sys.exit(2) urls = [] # Url to download from. Read from config file captions = [] # Captions for each image. Read from config file filenames = [] # Temporary file names config_fp = open(CONFIG_FILE, "r") line = config_fp.readline() N=0 while line: params = line.split(":::") urls.append(params[0].strip()) captions.append(params[1].strip()) line = config_fp.readline() N+=1 config_fp.close() # Download each url. This is how to iterate over mutliple lists at once for url, caption in zip(urls, captions): url_fp = urllib2.urlopen(url) # The following two lines are the preffered way to open a temporary # file. fd, name = tempfile.mkstemp() img_fp = os.fdopen(fd, "w") filenames.append(name) CopyFile(url_fp, img_fp) url_fp.close() img_fp.close() # The data won"t appear in img_fp until it"s closed # Use the ImageMagick suite from the shell to add a caption os.system("convert "%s" -set comment "%s" "%s"" % (name, caption, name)) # Figure out the parameters tilex, tiley = GetTiles(N) imgx, imgy = GetImageSize(tilex, tiley) # Use ImageMagick again to assemble the wallpaper cmd = "montage -geometry %dx%d -tile %dx%d -set caption "%%c" -pointsize 32 -size %dx%d -texture plasma: +polaroid -background black " % (imgx, imgy, tilex, tiley, SCREEN_X, SCREEN_Y) # Add each image file to the command string for filename in filenames: cmd += ""%s" " % filename # Add the output file to the command string and execute cmd += TARGET os.system(cmd) # Cleanup for filename in filenames: os.unlink(filename) # In ksh: rm $filename |