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

Display an image in Apache 2

Fri Sep 26, 2014 2:31 pm

Hello,

I have a probleme to display an image captured from picamera to a Apache 2 page :

In an index page (index.py) I can display a JPG image from a file.
But as soon as I want to display a JPG image from a capture with picamera, I have an error :
[error] [client 192.168.1.71] malformed header from script. Bad header=* failed to open vchiq instanc: index.py (from error.log file)

If someone has an idea of the error(s) made, thank you for your help.

Here is the index file:

Code: Select all

#!/usr/bin/env python

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

a=aide2.lecture()

sys.stdout.write("Content-type: image/jpeg\n\n")
sys.stdout.write(a)
Here is the function to read a JPG file:

Code: Select all

#!/usr/bin/env python

import sys
import time
import io

def	lecture():
	# image read
	fichier=io.BytesIO()
	fichier=open("Impact2.jpg","rb")
	objet=fichier.read()
	fichier.close()

	return objet
and here is the function to capture a JPG image:

Code: Select all

#!/usr/bin/env python

import sys
import time
import io
import picamera


def 	lecture():
	# image capture

	my_stream=io.BytesIO()
	camera=picamera.PiCamera()
	camera.resolution=(640, 480)
	camera.capture(my_stream,format='jpeg')
	my_stream.seek(0)
	objet=my_stream.read()
	camera.close()

	return objet

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: Display an image in Apache 2

Sat Sep 27, 2014 12:49 am

I'm not sure this is the best way. But I can fix this with: sudo chmod o+rwx /dev/vchiq.

It's not sticky across a reboot so it probably needs a UDEV rule to fix that.

sudo -i
echo 'SUBSYSTEM=="vchiq",GROUP="video",MODE="0660"' > /etc/udev/rules.d/10-vchiq-permissions.rules
usermod -a -G video www-data
exit
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: Display an image in Apache 2

Sat Sep 27, 2014 9:33 pm

Thank you Dougie,
With your solution, now I'm able to display an image with picamera in Apache 2.
Thank you again Dougie. :D

Return to “Python”