Skip to content

Instantly share code, notes, and snippets.

@richrd
Created November 22, 2014 17:37
Show Gist options
  • Save richrd/2ac9e743857f54a304a1 to your computer and use it in GitHub Desktop.
Save richrd/2ac9e743857f54a304a1 to your computer and use it in GitHub Desktop.
#
# HTTP Server that returns an image from the RaspberryPi camera
#
import io
import time
import picamera
import pyco_http
def get_image():
# Create the in-memory stream
stream = io.BytesIO()
camera = picamera.PiCamera()
camera.resolution = (360, 240)
camera.capture(stream, format='jpeg')
camera.close()
stream.seek(0)
return stream
def handle_request(request):
stream = get_image()
response = {
"status": 200,
"headers": {"Content-Type": "image/jpeg"},
"data": stream.read()
}
return response
if __name__ == "__main__":
srv = pyco_http.PycoHTTP()
srv.set_handler(handle_request)
srv.start(True) # True for blocking server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment