#!/usr/bin/python # sxsw.py - version 0.1 # Requires: python-imaging, qrencode, python-inotify # Author: Luke Macken # License: GPLv3 import os import Image import urllib import simplejson from os.path import join, basename, expanduser from pyinotify import WatchManager, Notifier, EventsCodes, ProcessEvent # Directory to watch for new images watch = expanduser('~/Desktop/images') # Where to spit out our qrcode, watermarked image, and local html out = expanduser('~/Desktop/sxsw') # The watermark to apply to all images watermark_img = expanduser('~/Desktop/fedora.png') # This assumes ssh-agent is running so we can do password-less scp ssh_image_repo = 'fedorapeople.org:~/public_html/sxsw/' # The public HTTP repository for uploaded images http_image_repo = 'http://lmacken.fedorapeople.org/sxsw/' # Size of the qrcode pixels qrcode_size = 10 def watermark(image): """ Apply a watermark to an image """ mark = Image.open(watermark_img) im = Image.open(image) if im.mode != 'RGBA': im = im.convert('RGBA') layer = Image.new('RGBA', im.size, (0,0,0,0)) position = (im.size[0] - mark.size[0], im.size[1] - mark.size[1]) layer.paste(mark, position) outfile = join(out, basename(image)) Image.composite(layer, im, layer).save(outfile) return outfile def upload(image): """ Upload this image to a remote server """ os.system('scp %r %s' % (image, ssh_image_repo)) return http_image_repo + basename(image) def qrencode(url): """ Generate a QRCode for a given URL """ qrcode = join(out, 'qrcode.png') os.system('qrencode -s %d -o %r %s' % (qrcode_size, qrcode, url)) return qrcode def tinyurl(url): """ Generate a tinyurl for a given URL """ data = urllib.urlopen("http://json-tinyurl.appspot.com/?url=%s" % url).read() json = simplejson.loads(data) if not json['ok']: print 'ERROR: There was a problem generating a tinyurl' return url return json['tinyurl'] def html_output(image, qrcode, tinyurl): """ Output HTML with the image, qrcode, and tinyurl """ html = """
%(tinyurl)s
""" % {'image': image, 'qrcode': qrcode, 'tinyurl': tinyurl} outfile = join(out, basename(image) + '.html') output = file(outfile, 'w') output.write(html) output.close() return outfile class DirectoryWatcher(ProcessEvent): def process_IN_MOVED_TO(self, event): filename = join(event.path, event.name) print "Processing %s..." % filename print "Applying watermark..." image = watermark(filename) print "Uploading to remote server..." url = upload(image) print "Generating QRCode..." qrcode = qrencode(url) print "Generating TinyURL..." tiny = tinyurl(url) print "Generating HTML..." html = html_output(url, qrcode, tiny) print "Done!" os.system('firefox %r' % html) process_IN_CREATE = process_IN_MOVED_TO def watch_directory(): wm = WatchManager() notifier = Notifier(wm, DirectoryWatcher()) mask = EventsCodes.ALL_FLAGS['IN_MOVED_TO'] | EventsCodes.ALL_FLAGS['IN_CREATE'] wdd = wm.add_watch(watch, mask) print "Monitoring %s for new images..." % watch while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break if __name__ == "__main__": watch_directory()