From c84779c840f29346115906ea4fb7cf7b40f29ea3 Mon Sep 17 00:00:00 2001 From: Luke Macken Date: Fri, 4 Mar 2011 13:52:33 -0500 Subject: Initial commit --- photobooth.py | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 photobooth.py diff --git a/photobooth.py b/photobooth.py new file mode 100644 index 0000000..93fee0a --- /dev/null +++ b/photobooth.py @@ -0,0 +1,137 @@ +#!/usr/bin/python +# sxsw.py - version 0.2 +# Requires: python-imaging, qrencode, gphoto2 +# Author: Luke Macken +# License: GPLv3 + +import os +import Image +import urllib +import simplejson +import subprocess + +from uuid import uuid4 +from os.path import join, basename, expanduser + +# 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 + +# Whether or not to delete the photo after uploading it to the remote server +delete_after_upload = True + +# The camera configuration +# Use gphoto2 --list-config and --get-config for more information +gphoto_config = { + '/main/imgsettings/imagesize': 3, # small + '/main/imgsettings/imagequality': 0, # normal +} + +class PhotoBooth(object): + + def capture_photo(self): + """ Capture a photo and download it from the camera """ + filename = join(out, '%s.jpg' % str(uuid4())) + cfg = ['--set-config=%s=%s' % (k, v) for k, v in gphoto_config.items()] + subprocess.call('gphoto2 --auto-detect ' + + '--capture-image-and-download ' + + '--filename="%s" ' % filename + + ' '.join(cfg), shell=True) + return filename + + def process_image(self, filename): + print "Processing %s..." % filename + print "Applying watermark..." + image = self.watermark(filename) + print "Uploading to remote server..." + url = self.upload(image) + print "Generating QRCode..." + qrcode = self.qrencode(url) + print "Generating TinyURL..." + tiny = self.tinyurl(url) + print "Generating HTML..." + html = self.html_output(url, qrcode, tiny) + subprocess.call('firefox "%s"' % html, shell=True) + print "Done!" + + def watermark(self, 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(self, image): + """ Upload this image to a remote server """ + os.system('scp "%s" %s' % (image, ssh_image_repo)) + if delete_after_upload: + os.unlink(image) + return http_image_repo + basename(image) + + def qrencode(self, url): + """ Generate a QRCode for a given URL """ + qrcode = join(out, 'qrcode.png') + os.system('qrencode -s %d -o "%s" %s' % (qrcode_size, qrcode, url)) + return qrcode + + def tinyurl(self, 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' + print json + return url + return json['tinyurl'] + + def html_output(self, 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 + +if __name__ == "__main__": + photobooth = PhotoBooth() + try: + while True: + raw_input("Press enter to capture photo.") + filename = photobooth.capture_photo() + photobooth.process_image(filename) + except KeyboardInterrupt: + print "\nExiting..." -- cgit