To use a camera with the Raspberry PI, first [configure the Raspberry PI] to enable the camera.
In order to send large images from the Raspberry PI camera to a web server, the image has to be sent in parts. This requires the Python `poster` module. https://pypi.python.org/pypi/poster/
To be able to install a python module, `pip` has to be installed.
sudo pip install poster
This is the server PHP page that saves the image.
save_image.php
<?php
$image = $_FILES["image"];
if ($image == null) {
echo "Missing image to save!";
} else {
echo "Saved image!";
$tmp_name = $_FILES["image"]["tmp_name"];
move_uploaded_file($tmp_name, "image.jpg");
}
?>
This Python script sends the image to the server.
save_image.py
#!/usr/bin/env python
import urllib, urllib2
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
register_openers()
url = "http://domain.com/path/save_image.php"
print "url="+url
filename = 'image.jpg';
if (os.path.isfile(filename)) :
values = {'image':open(filename)}
data, headers = multipart_encode(values)
headers['User-Agent'] = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
req = urllib2.Request(url, data, headers)
req.unverifiable = True
content = urllib2.urlopen(req).read()
print (content)
else:
print 'No image file found to upload';
print '\r\nProgam complete.'
This script will capture from the camera and then call the above script to upload the image.
capture_image.py
#get access to the camera
from picamera import PiCamera
#import so we can invoke another script
import subprocess
#sleep so we can wait on the camera
from time import sleep
# create a camera object
camera = PiCamera()
#set the image resolution
camera.resolution = (320, 240)
#rotate the camera if upside-down
camera.rotation = 180
#show preview with some transparency
camera.start_preview(alpha=225)
#wait two seconds for camera lighting
sleep(2)
#save image locally
camera.capture('image.jpg')
#stop the preview
camera.stop_preview()
#invoke the script to upload the image
subprocess.call('python save_image.py', shell=True)