Tom T
Posts: 6
Joined: Sat Aug 09, 2014 3:13 pm

How to refresh an image with CGI ?

Wed Oct 01, 2014 9:58 pm

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.

User avatar
DougieLawson
Posts: 39297
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to refresh an image with CGI ?

Wed Oct 01, 2014 10:06 pm

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Tom T
Posts: 6
Joined: Sat Aug 09, 2014 3:13 pm

Re: How to refresh an image with CGI ?

Thu Oct 02, 2014 4:15 pm

Thank you angain Dougie for your help. :D

Return to “Python”