#!/usr/bin/python # photobooth.py - version 0.3 # Requires: python-imaging, qrencode, gphoto2, surl # Author: Luke Macken # License: GPLv3 import os import surl import Image 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/oscon') # The watermark to apply to all images watermark_img = expanduser('~/Desktop/watermark-fedora.png') # This assumes ssh-agent is running so we can do password-less scp ssh_image_repo = 'fedorapeople.org:/srv/groups/photobooth' # The public HTTP repository for uploaded images http_image_repo = 'http://fedorapeople.org/groups/photobooth/' # 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 = False # 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 '/main/capturesettings/zoom': 70, # zoom factor } # The URL shortener to use shortener = 'tinyurl.com' class PhotoBooth(object): def initialize(self): """ Detect the camera and set the various settings """ cfg = ['--set-config=%s=%s' % (k, v) for k, v in gphoto_config.items()] subprocess.call('gphoto2 --auto-detect ' + ' '.join(cfg), shell=True) 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 ' + '--capture-image-and-download ' + '--filename="%s" ' % filename, shell=True) return filename def process_image(self, filename): print "Processing %s..." % filename print "Applying watermark..." image = self.watermark(filename) print "Uploading image to remote server..." image_url = self.upload(image) print "Generating and Uploading fancy HTML to remote server..." url = self.fancy_html_output(image) print "Generating QRCode..." qrcode = self.qrencode(url) print "Shortening URL..." tiny = self.shorten(url) print "Generating HTML..." html = self.html_output(image_url, qrcode, tiny) subprocess.call('xdg-open "%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 """ subprocess.call('scp "%s" %s' % (image, ssh_image_repo), shell=True) 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') subprocess.call('qrencode -s %d -o "%s" %s' % ( qrcode_size, qrcode, url), shell=True) return qrcode def shorten(self, url): """ Generate a shortened URL """ return surl.services.supportedServices()[shortener].get({}, url) def fancy_html_output(self, image): """ Output Fancy HTML page to upload """ fancy_html = """

Download Your Fedora Penguin Photo!

penguin pic

Right click the photo and select "Save as..." to download your photo.

Thanks for stopping by the Fedora OSCON Photobooth! Tux really enjoyed meeting you.

Fedora is a free and open-source operating system. It comes with all sorts of great software out of the box, and you can try it live without installing it.

Why not download Fedora and give it a shot?

Fedora is built by a welcoming community of users known as the Fedora Project, who work to make their own software better. It's fun and rewarding. No matter what your skill-set, you can $

Why not join us and make the world a better place?

""" % {'image': image} fancy_outfile = join(out, image + '.html') fancy_output = file(fancy_outfile, 'w') fancy_output.write(fancy_html) fancy_output.close() subprocess.call('scp "%s" %s' % (fancy_html, ssh_image_repo), shell=True) return http_image_repo + image + '.html' 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: photobooth.initialize() while True: raw_input("Press enter to capture photo.") # filename = photobooth.capture_photo() filename = join(out, 'dragicorn.jpg') photobooth.process_image(filename) except KeyboardInterrupt: print "\nExiting..."