HomeRaspberry Pi › Photo
Summary | Requirements | Hardware | Software | Feedback | History

Raspberry Pi Camera Photo

How to take a photo with your Raspberry Pi and its camera board and retrieve the image from another computer using scp. Mac and Linux computers have the necessary tools built-in, on Windows use Cygwin.

top↑

Summary

Make a Bash script on the Raspberry Pi as a wrapper around raspistill. Calculate width and height, save image to RAM disk, output name of saved file. Create another shell script on a connected computer. SSH to the Raspberry Pi and call the Bash script to take a photo. Capture the remote file name. Use SCP to transfer the image. Delete the remote file.

top↑

Requirements

[TODO]

top↑

Hardware

[TODO]

top↑

Software

[TODO: ssh-keygen]

Save as /usr/local/bin/takephoto on your Raspberry Pi and make executable with sudo chmod 755 /usr/local/bin/takephoto:

#!/bin/bash

declare -i DEFW=1600  # Standard image width
declare -i MINW=16    # Minimum image width
declare -i MAXW=2592  # Maximum image width (sensor size = 2592 x 1944)
declare -i MAXH=1944  # Maximum image height

ASP="4:3"  # Aspect ratio 16:10, 16:9 (HD) or 1:1 (square), otherwise 4:3 (SD)
APP="/usr/bin/raspistill -t 2000 -n -e jpg -q 80"  # How to call raspistill
DIR="/dev/shm/"  # Save to RAM disk

# Image width
declare -i W
if [[ "$1" == "" ]]; then
	W=$DEFW
elif (( $1 <= MINW )); then
	W=$MINW
elif (( $1 >= MAXW )); then
	W=$MAXW
else
	# Truncate to multiple of aspect ratio width
	if [[ $ASP == "16:9" ]]; then
		(( W = $1 / 16 * 16 ))
	elif [[ $ASP == "16:10" ]]; then
		(( W = $1 / 8 * 8 ))
	elif [[ $ASP != "1:1" ]]; then
		(( W = $1 / 4 * 4 ))
	fi
fi

# Image height
declare -i H
if [[ $ASP == "16:9" ]]; then
	(( H = W / 16 * 9 ))
elif [[ $ASP == "16:10" ]]; then
	(( H = W / 8 * 5 ))
elif [[ $ASP == "1:1" ]]; then
	H=$W
	# Only chance of height being out of bounds
	if (( H > MAXH )); then
		H=MAXH
		W=MAXH
	fi
else
	# Standard aspect ratio 4:3
	(( H = W / 4 * 3 ))
fi

FILENAME="${DIR}${W}x${H}.jpg"
$APP -w $W -h $H -o "$FILENAME"
echo "$FILENAME"  # To capture name of saved file on remote computer
exit 0

Save as ~/bin/picam on your Mac and make executable with chmod 700 ~/bin/picam:

#!/bin/bash

REMOTEHOST=raspberrypi
LOCALFILE="$HOME/Desktop/picam.jpg"  # Save to local desktop
PREVIEW=0  # Open Preview.app? 0 = No, 1 = Yes

REMOTEFILE=$(ssh -x $REMOTEHOST takephoto "$*")
QREMOTEFILE=$(printf %q "$REMOTEFILE")
scp -p "$REMOTEHOST:$QREMOTEFILE" "$LOCALFILE"
ssh -x $REMOTEHOST rm "$QREMOTEFILE"

if (( PREVIEW )); then
	if [[ -r "$LOCALFILE" ]]; then
		open -a /Applications/Preview.app "$LOCALFILE"
	fi
fi
exit 0

Run in Terminal on the Mac as picam or e.g. picam 800. Optional number is the image width. Image height is calculated from this and the aspect ratio set in the takephoto script.

top↑

Feedback

Cheers, boos and questions via the Raspberry Pi Forum.

top↑

Version History

2013-08-19
Initial release. Source code: remote | local