Page 1 of 1

How to refresh an image with CGI ?

Posted: Wed Oct 01, 2014 9:58 pm
by Tom T
Hello,

I try to realise a web server where a captured picture is visible with a refresh every 1 second.
Only python is used (with Picamera library) . And Apache2 is used as http server.

2 days ago I tried to use a loop to refresh the image, but I learned that the page is sent when all the script is read (Means only at the end of the loop).

Today I don't know how it is possible to refresh the image periodically every second.
Each time I press the refresh button of the browser I use (chrome, Mozilla), I have a new image which comes from a new capture. (manual refresh of the all page)

Here is the index page:

Code: Select all

#!/usr/bin/env python

import cgi
import cgitb
cgitb.enable()
import sys
import time
import io
import aide4

sys.stdout.write("Content-type: image/jpeg\n\n")

aide4.lecture()
And here is the aide4.py page where the lecture function is coded:

Code: Select all

#!/usr/bin/env python
import cgi
import cgitb
cgitb.enable()
import sys
import time
import io
import picamera

def	lecture():
	my_stream=io.BytesIO()
	camera=picamera.PiCamera()
	camera.resolution=(1296, 972)
	camera.capture(my_stream, format='jpeg')
	my_stream.seek(0)
	sys.stdout.write(my_stream.read())
	sys.stdout.flush()
	camera.close()
How is it possible to refresh automatically this image ?

Thank you for your help.

Re: How to refresh an image with CGI ?

Posted: Wed Oct 01, 2014 10:06 pm
by DougieLawson
Add this http header

Code: Select all

sys.stdout.write("Refresh: 1; url=http://pi.local/whateveryourURLishere\n")
with the URL you're using for your image.

You may also want

Code: Select all

sys.stdout.write("Expires: Wed, 01 Oct 2014 22:07:00 GMT")
so that the image expires one minute after it is created.

Re: How to refresh an image with CGI ?

Posted: Thu Oct 02, 2014 4:15 pm
by Tom T
Thank you angain Dougie for your help. :D