I have a Pi with a camera module that I control with Python.
I created a web server with web.py that allows me to make HTTP requests to the PI to retrieve an image. There are two optional parameters to specify the width and height of the image. The code looks like this (
urls = ( '/capture', 'Capture' ) # start with default values for resolution width = 1024 height = 768 if __name__ == "__main__": app = web.application(urls, globals()) app.run() class Capture: def GET(self): input = web.input(w="0", h="0") # reading user input of resolution values for input.w = int(input.w) # to integer input.h = int(input.h) if input.w == 0: input.w = width # no width given, use saved one else: input.w = max(0,min(2592, input.w)) # clamp to range of valid values if input.h == 0: input.h = height # no height given, use saved one else: input.h = max(0,min(1944, input.h)) # clamp to range of valid values camera = picamera.PiCamera() # camera camera.resolution = (input.w, input.h) camera.capture('picture.jpg') # capture image to file camera.close() web.header('Content-Type', 'image/jpg') return open('picture.jpg',"rb").read() # read image from file and respond
I understand that the code handling the width and height is not very elegant and could be shorter.
To get an image, I can enter
http://<PI IP address>/capture in a browser. The problem is that it takes quite a while (1311ms): waiting 1264ms and receiving 47ms.
The results are obviously the same on different browsers because this is a server-side issue.
How can I get the image more quickly?
I think saving to an image file first and reopening that to get the return value is redundant (and possibly slow), but I don't know how to make the
capture() function return the value directly (so that I could return it to the client directly). I do not necessarily need jpeg format.