
My recent DIY electronics project has been putting together a Raspberry Pi-based camera. The Pi foundation sells a camera board which plugs into the Pi; it’s sold as ‘a bit better than the average camera in a mobile phone.’ But the Pi’s default Raspbian Linux installation comes with a couple programs for controlling the camera, and lets you take still pictures and videos easily.
In the interest of using the camera for something you can’t usually do with a store-bought digital camera, I wrote a short python script which takes a photo assigns it a file name based on the date and time taken. It then does some sampling of the picture, and only keeps pictures which aren’t ‘too dark.’ And then cron runs the photo script once every five minutes. In other words, the Pi is set up for long-form time-lapse photography. The resulting pictures are then easy to compile into a movie.
We left the camera in the dining room of my partner’s house for about three weeks; here’s a video with about eight days worth of footage. (The rest is kinda boring, so I trimmed it to the length of the song.)
The plan is to put the Pi somewhere really awesome over the winter and capture lots of footage for six months or so… (We know exactly where it’s going, but I’ll leave it as a surprise for… June?) Making this a kind of low-work long-term project, I guess. Kind of like brewing, come to think of it: Put in some initial work, then just wait a long long time for the yummy product. For the long-term winter placement, I’m experimenting with using two Pi’s with two camera boards to capture 3D pictures. And I kinda want to hang out at HackLab long enough to figure out how to get the Pi to run some Mindstorm robotics to do things like really slow pans, but I so don’t have the time right now…
For those who might want to try something like this at home, here’s the full script I’m using.
import subprocess import Image import time import random def rsample(im, nsamples=500): #Probabalistic. (w,h)=im.size avg=0.0 for i in xrange(nsamples): p=im.getpixel((random.randint(0,w-1), random.randint(0,h-1))) avg+=float(p[0]+p[1]+p[2])/3 avg=avg/nsamples return avg def fullsample(im): #Average the entire image. Much costlier for time! (w,h)=im.size avg=0.0 for p in im.getdata(): avg+=float(p[0]+p[1]+p[2])/3 l=float(w*h) avg=avg/l return avg dtime=subprocess.check_output(['date', '+%y%m%d_%T']).strip() dtime=dtime.replace(':', '.') filename='/home/pi/pictures/pipic_'+dtime+'.jpg' w=str(int(2592/2)) h=str(int(1944/2)) options='-ex auto -w '+w+' -h '+h+' -o /home/pi/pictures/new.jpg' print options subprocess.call('raspistill '+options, shell=True) time.sleep(1)#Get average 'brightness' of the image. im=Image.open("/home/pi/pictures/new.jpg") avg=rsample(im,500) print 'Average brightness of the image: ', avg if avg<5: print 'Image is very dark.' else: subprocess.call('convert -rotate 180 /home/pi/pictures/new.jpg '+filename, shell=True)
The dependencies are the Python imaging library (which you can install with ‘apt-get install python-imaging
‘), and there’s also some trivial use of ‘convert
‘ which is part of the ImageMagick package. ImageMagick is kind of amazing, and well worth checking out if you’re doing anything with batch-processing of images, by the way.