fork download
  1. #!/usr/bin/python
  2. # photobooth.py - version 0.3
  3. # Requires: python-imaging, qrencode, gphoto2, surl
  4. # Author: Luke Macken <lmacken@redhat.com>
  5. # License: GPLv3
  6.  
  7. import os
  8. import surl
  9. import Image
  10. import subprocess
  11. import Image
  12.  
  13. from uuid import uuid4
  14. from os.path import join, basename, expanduser
  15.  
  16. # Where to spit out our qrcode, watermarked image, and local html
  17. out = expanduser('~/Desktop/sxsw')
  18.  
  19. # The watermark to apply to all images
  20. watermark_img = expanduser('~/Desktop/fedora.png')
  21.  
  22. # This assumes ssh-agent is running so we can do password-less scp
  23. ssh_image_repo = 'fedorapeople.org:~/public_html/sxsw/'
  24.  
  25. # The public HTTP repository for uploaded images
  26. http_image_repo = 'http://l...content-available-to-author-only...e.org/sxsw/'
  27.  
  28. # Size of the qrcode pixels
  29. qrcode_size = 10
  30.  
  31. # Whether or not to delete the photo after uploading it to the remote server
  32. delete_after_upload = True
  33.  
  34. # The camera configuration
  35. # Use gphoto2 --list-config and --get-config for more information
  36. gphoto_config = {
  37. '/main/imgsettings/imagesize': 3, # small
  38. '/main/imgsettings/imagequality': 0, # normal
  39. '/main/capturesettings/zoom': 70, # zoom factor
  40. }
  41.  
  42. # The URL shortener to use
  43. shortener = 'tinyurl.com'
  44.  
  45. class PhotoBooth(object):
  46.  
  47. def initialize(self):
  48. """ Detect the camera and set the various settings """
  49. cfg = ['--set-config=%s=%s' % (k, v) for k, v in gphoto_config.items()]
  50. subprocess.call('gphoto2 --auto-detect ' +
  51. ' '.join(cfg), shell=True)
  52.  
  53. def capture_photo(self):
  54. """ Capture a photo and download it from the camera """
  55. imagefile = join(out, '%s.jpg' % str(uuid4()))
  56. cfg = ['--set-config=%s=%s' % (k, v) for k, v in gphoto_config.items()]
  57. subprocess.call('gphoto2 ' +
  58. '--capture-image-and-download ' +
  59. '--imagefile="%s" ' % imagefile,
  60. shell=True)
  61. return imagefile
  62.  
  63. def resize_image(self, image, imagefile):
  64. im1 = Image.open(imagefile)
  65. width = 500
  66. height = 420
  67. im2 = im1.resize((width, height), Image.ANTIALIAS)
  68. ext = ".jpg"
  69. im2.save("ANTIALIAS" + ext)
  70. filename = %s" % "ANTIALIAS.jpg
  71. return filename
  72.  
  73. def process_image(self, filename):
  74. print "Processing %s..." % filename
  75. print "Applying watermark..."
  76. image = self.watermark(filename)
  77. print "Uploading to remote server..."
  78. url = self.upload(image)
  79. print "Generating QRCode..."
  80. qrcode = self.qrencode(url)
  81. print "Shortening URL..."
  82. tiny = self.shorten(url)
  83. print "Generating HTML..."
  84. html = self.html_output(url, qrcode, tiny)
  85. subprocess.call('xdg-open "%s"' % html, shell=True)
  86. print "Done!"
  87.  
  88. def watermark(self, image):
  89. """ Apply a watermark to an image """
  90. mark = Image.open(watermark_img)
  91. im = Image.open(image)
  92. if im.mode != 'RGBA':
  93. im = im.convert('RGBA')
  94. layer = Image.new('RGBA', im.size, (0,0,0,0))
  95. position = (im.size[0] - mark.size[0], im.size[1] - mark.size[1])
  96. layer.paste(mark, position)
  97. outfile = join(out, basename(image))
  98. Image.composite(layer, im, layer).save(outfile)
  99. return outfile
  100.  
  101. def upload(self, image):
  102. """ Upload this image to a remote server """
  103. subprocess.call('scp "%s" %s' % (image, ssh_image_repo), shell=True)
  104. if delete_after_upload:
  105. os.unlink(image)
  106. return http_image_repo + basename(image)
  107.  
  108. def qrencode(self, url):
  109. """ Generate a QRCode for a given URL """
  110. qrcode = join(out, 'qrcode.png')
  111. subprocess.call('qrencode -s %d -o "%s" %s' % (
  112. qrcode_size, qrcode, url), shell=True)
  113. return qrcode
  114.  
  115. def shorten(self, url):
  116. """ Generate a shortened URL """
  117. return surl.services.supportedServices()[shortener].get({}, url)
  118.  
  119. def html_output(self, image, qrcode, tinyurl):
  120. """ Output HTML with the image, qrcode, and tinyurl """
  121. html = """
  122. <html>
  123. <center>
  124. <table>
  125. <tr>
  126. <td colspan="2">
  127. <b><a href="%(tinyurl)s">%(tinyurl)s</a></b>
  128. </td>
  129. </tr>
  130. <tr>
  131. <td><img src="%(image)s" border="0"/></td>
  132. <td><img src="%(qrcode)s" border="0"/></td>
  133. </tr>
  134. </table>
  135. </center>
  136. </html>
  137. """ % {'image': image, 'qrcode': qrcode, 'tinyurl': tinyurl}
  138. outfile = join(out, basename(image) + '.html')
  139. output = file(outfile, 'w')
  140. output.write(html)
  141. output.close()
  142. return outfile
  143.  
  144. if __name__ == "__main__":
  145. photobooth = PhotoBooth()
  146. try:
  147. photobooth.initialize()
  148. while True:
  149. raw_input("Press enter to capture photo.")
  150. filename = photobooth.capture_photo()
  151. photobooth.process_image(filename)
  152. except KeyboardInterrupt:
  153. print "\nExiting..."
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:2: error: invalid preprocessing directive #!
prog.cpp:2:3: error: invalid preprocessing directive #photobooth
prog.cpp:3:3: error: invalid preprocessing directive #Requires
prog.cpp:4:3: error: invalid preprocessing directive #Author
prog.cpp:5:3: error: invalid preprocessing directive #License
prog.cpp:16:3: error: invalid preprocessing directive #Where
prog.cpp:17:18: warning: character constant too long for its type
prog.cpp:19:3: error: invalid preprocessing directive #The
prog.cpp:20:28: warning: character constant too long for its type
prog.cpp:22:3: error: invalid preprocessing directive #This
prog.cpp:23:18: warning: character constant too long for its type
prog.cpp:25:3: error: invalid preprocessing directive #The
prog.cpp:26:19: warning: character constant too long for its type
prog.cpp:28:3: error: invalid preprocessing directive #Size
prog.cpp:31:3: error: invalid preprocessing directive #Whether
prog.cpp:34:3: error: invalid preprocessing directive #The
prog.cpp:35:3: error: invalid preprocessing directive #Use
prog.cpp:37:5: warning: character constant too long for its type
prog.cpp:37: error: stray ‘#’ in program
prog.cpp:38:5: warning: character constant too long for its type
prog.cpp:38: error: stray ‘#’ in program
prog.cpp:39:5: warning: character constant too long for its type
prog.cpp:39: error: stray ‘#’ in program
prog.cpp:42:3: error: invalid preprocessing directive #The
prog.cpp:43:13: warning: character constant too long for its type
prog.cpp:49:16: warning: character constant too long for its type
prog.cpp:50:25: warning: character constant too long for its type
prog.cpp:55:31: warning: character constant too long for its type
prog.cpp:56:16: warning: character constant too long for its type
prog.cpp:57:25: warning: character constant too long for its type
prog.cpp:58:25: warning: character constant too long for its type
prog.cpp:59:25: warning: character constant too long for its type
prog.cpp:85:25: warning: character constant too long for its type
prog.cpp:92:23: warning: multi-character character constant
prog.cpp:93:29: warning: multi-character character constant
prog.cpp:94:27: warning: multi-character character constant
prog.cpp:103:25: warning: character constant too long for its type
prog.cpp:110:28: warning: character constant too long for its type
prog.cpp:111:25: warning: character constant too long for its type
prog.cpp:121:18: warning: missing terminating " character
prog.cpp:121: error: missing terminating " character
prog.cpp:137:3: warning: missing terminating " character
prog.cpp:137: error: missing terminating " character
prog.cpp:138:47: warning: character constant too long for its type
prog.cpp:7: error: ‘import’ does not name a type
prog.cpp:43: error: expected constructor, destructor, or type conversion before ‘=’ token
prog.cpp:117: error: expected unqualified-id before ‘,’ token
prog.cpp:117: error: expected constructor, destructor, or type conversion before ‘)’ token
stdout
Standard output is empty