#!/usr/bin/env python #Choose a image with random generator #and convert it to ppm #This can be used for KDE to have a different #background image every N minutes. #In KDE Config: /.../background-image.py --size %xx%y %f import os import re import sys import time import getopt import random from PIL import Image mydir=os.path.join(os.environ["HOME"], "pictures", "ixus") regex=re.compile(r'IMG_\d+.(JPG|jpg)$') def usage(): sys.stderr.write("""Usage: %s --size WxH outfile\n""" % ( os.path.basename(sys.argv[0]))) def main(): try: opts, args = getopt.getopt(sys.argv[1:], "", ["size="]) except: usage() sys.exit(1) size=None for o, a in opts: if o=="--size": size=a if not size: usage() sys.exit() if (len(args)!=1): usage() sys.exit(1) outfile=args[0] os.nice(20) match=re.match(r'^(\d+)x(\d+)$', size) if not match: print "%s is no size (WxH)" % size usage() sys.exit() width=int(match.group(1)) height=int(match.group(2)) imgfiles=[] for root, dirs, files in os.walk(mydir): for file in files: if regex.match(file): imgfiles.append(os.path.join(root, file)) if not imgfiles: raise("No files found in %s" % mydir) file=random.choice(imgfiles) img=Image.new("RGB", (width, height), color=0) fg=Image.open(file) fg.load() fg.thumbnail((width, height), Image.ANTIALIAS) twidth, theight = fg.size bl= (width - twidth) / 2 # border left bu = (height - theight) / 2 # border upper img.paste(fg, (bl, bu)) img.save(outfile, format="ppm") fd=open("/tmp/bi.log", "a") fd.write("%s %s %s\n" % (time.strftime("%Y-%m-%d %H:%M:%S"), file, outfile)) fd.close() if __name__=="__main__": main()