Skip to content

Instantly share code, notes, and snippets.

@bitdivision
Last active August 29, 2015 14:00
Show Gist options
  • Save bitdivision/11367058 to your computer and use it in GitHub Desktop.
Save bitdivision/11367058 to your computer and use it in GitHub Desktop.
Simple DSLR MJPEG Server
#!/usr/bin/python2.7
'''
Author: Igor Maculan - [email protected]
A Simple mjpg stream http server
Modified for DSLR interface by Zoetrope Ltd.
'''
from PIL import Image
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import StringIO
import time
import camera
capture=None
class CamHandler(BaseHTTPRequestHandler):
def grabFrameAndDisplay(self):
capture.preview_to_file("preview.jpg")
jpg = Image.open("preview.jpg")
tmpFile = StringIO.StringIO()
jpg.save(tmpFile,'JPEG')
self.wfile.write("--jpgboundary")
self.send_header('Content-type','image/jpeg')
self.send_header('Content-length',str(tmpFile.len))
self.end_headers()
def do_GET(self):
if self.path.endswith('.mjpg'):
self.send_response(200)
self.send_header('Content-type','multipart/x-mixed-replace; boundary=--jpgboundary')
self.end_headers()
while True:
try:
self.grabFrameAndDisplay()
except KeyboardInterrupt:
break
return
if self.path.endswith('.html'):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write('<html><head></head><body>')
self.wfile.write('<img src="cam.mjpg"/>')
self.wfile.write('</body></html>')
return
def main():
global capture
capture = camera.Camera()
try:
server = HTTPServer(('',9000),CamHandler)
print "server started"
server.serve_forever()
except KeyboardInterrupt:
capture.release()
server.socket.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment