Time-lapse pho­tog­ra­phy.....

Les Pounder re­veals how it’s pos­si­ble to cre­ate a straight­for­ward time­lapse con­troller for the pi­cam­era that’s run­ning an ana­logue con­trol sys­tem.

Linux Format - - CONTENTS -

Learn how to take bet­ter time-lapse pho­to­graphs by read­ing Les Pounder’s handy project.

Us­ing the Rasp­berry Pi cam­era for time­lapse is a sim­ple project, but en­sur­ing that the sub­ject of your pho­tog­ra­phy is lit cor­rectly can be a lit­tle tricky. In this project we shall cre­ate a time­lapse con­troller that has ana­logue con­trollers for the bright­ness and con­trast of the im­ages to be cap­tured. The files saved will also have a time­stamp to en­sure that they’re cap­tured cor­rectly and can be re­viewed in chrono­log­i­cal or­der.

So let’s start by build­ing the hard­ware.

Cir­cuit city

To build the cir­cuit for this project we need to place an MCP3008 ana­logue-to-dig­i­tal con­verter onto the bread­board. Pay spe­cial at­ten­tion to a dip in one end of the chip, which iden­ti­fies the top of the chip. Pin 1, which is the first ana­logue in­put, is lo­cated in the top left of the chip if we look at the chip with the dip at the top.

Con­nect the MCP3008 to your Rasp­berry Pi as per the di­a­gram shown below. The po­ten­tiome­ters are con­nected to 3V and GND rails, and their out­put is fed into the first two pins of the MCP3008. Our push but­ton is con­nected to the GPIO and to the GND rail.

Next, in­sert the of­fi­cial Rasp­berry Pi cam­era into the cam­era port, which is lo­cated ei­ther be­tween the Eth­er­net and HDMI port of the model B se­ries, or on the end of the Pi Zero board – note that this re­quires a spe­cial adapter. The rib­bon ca­ble should be care­fully in­serted with the blue tab that’s fac­ing the Eth­er­net port, and re­mem­ber to lift the plas­tic guard on the port be­fore in­sert­ing, then close the guard to lock the cam­era in place. Ap­ply­ing a small blob of plas­ticine will pre­vent the cam­era from mak­ing con­tact with the GPIO pins, which could dam­age the cam­era. Now con­nect your key­board, mouse and so on, and power up your Rasp­berry Pi to the desk­top.

Get­ting started

The of­fi­cial Rasp­berry Pi cam­era and our MCP3008 both need their in­ter­faces con­fig­ur­ing. To do this go to the Rasp­berry Pi Con­fig­u­ra­tion tool, which is lo­cated in the Pref­er­ences menu. In the In­ter­faces tab, en­able the Cam­era and SPI in­ter­face. Save your con­fig­u­ra­tion and the ap­pli­ca­tion will now ask you to re­boot. Do so and re­turn to the Rasp­bian desk­top.

To test the cam­era, open the Ter­mi­nal and en­ter this com­mand to take a photo which will be out­put as im­age.jpg: $ raspis­till -o im­age.jpg

If this doesn’t work, re­peat the con­fig­u­ra­tion, power down the Pi and check that your ca­ble is in­serted cor­rectly.

Now to write some code. Go to the Pro­gram­ming menu and click Python 3. When the ap­pli­ca­tion opens click File>New to cre­ate a new blank file. Im­me­di­ately save the file (File>Save), as time­lapse-con­troller.py be­fore car­ry­ing on. We start the code by im­port­ing the li­braries that we will be us­ing. Th­ese are pi­cam­era, used to con­trol the cam­era; GPIO Zero for our ana­logue-to-dig­i­tal con­verter and the push but­ton; the sleep func­tion is im­ported from the time li­brary; and we’ll need date­time to cre­ate a time­stamp later. from pi­cam­era im­port Pi­Cam­era from gpi­ozero im­port MCP3008, But­ton from time im­port sleep from date­time im­port date­time

Now we need to set up the chan­nel con­nec­tions to the MCP3008, tell the GPIO where our push but­ton is con­nected and cre­ate an ob­ject from the pi­cam­era li­brary that will make it eas­ier to work with. pot1 = MCP3008(chan­nel=0) pot2 = MCP3008(chan­nel=1) but­ton = But­ton(17) cam­era = Pi­Cam­era()

To cap­ture our im­ages we shall cre­ate a func­tion that can be called later in our code. The func­tion, called cap­ture, starts with a for-loop that will it­er­ate for a user-con­trolled num­ber of times. The for-loop will cre­ate a time­stamp, the time and date at that mo­ment, and then use that value as a file­name to cap­ture an im­age. The loop then sleeps for another user­con­trolled du­ra­tion be­fore re­peat­ing.

def cap­ture(): for i in range(timer): time­stamp = date­time.now().iso­for­mat() cam­era.cap­ture('/home/pi/%s.jpg’ % time­stamp) sleep(de­lay)

In or­der to see what the cam­era sees, we cre­ate a pre­view win­dow. In this case this win­dow has a fixed res­o­lu­tion of 640x480 and is placed in the top left of the screen (0,0). But typ­i­cally the pre­view is fullscreen.

cam­era.start_pre­view(fullscreen=False, win­dow =(0, 0, 640, 480))

Try and try again

Us­ing a try, ex­cept, fi­nally con­struc­tion to han­dle any er­rors and the user ex­it­ing the code, we cre­ate two vari­ables: timer and de­lay. The timer vari­able will store the amount of time in min­utes that the time­lapse should run for. The de­lay vari­able is used to store the de­lay in sec­onds be­tween shots. Both vari­ables cap­ture user in­put from the key­board, and con­vert it to an in­te­ger. try:

timer = int(in­put("How long in min­utes should the time­lapse run for?"))

de­lay = int(in­put("How long is the de­lay, in sec­onds, be­tween shots?"))

Inside a while True loop, we now take the value of the po­ten­tiome­ter, typ­i­cally be­tween 0.0 and 1.0 and mul­ti­ply it by 100 to give us a value to con­trol the bright­ness and con­trast of the im­age. Turn­ing the po­ten­tiome­ters will change the value and print the new value to the shell. while True: bright­ness = round(pot1.value * 100) print("Bright­ness”,bright­ness) con­trast = round(pot2.value * 100) print("Con­trast”,con­trast) Use the bright­ness and con­trast vari­ables to up­date the pre­view win­dow, show­ing the change in con­fig­u­ra­tion. We then cre­ate the “set­tings” vari­able that con­tains both val­ues. cam­era.bright­ness = bright­ness cam­era.con­trast = con­trast set­tings = “Bright­ness: “+str(bright­ness)+” Con­trast: “+str(con­trast)

Th­ese set­tings are then printed to the Python shell, be­fore be­ing an­no­tated to the pre­view win­dow, typ­i­cally at the top cen­tre of the im­age. There’s a sleep step of 0.1 sec­ond be­fore the process is re­peated:

print(set­tings) cam­era.an­no­tate_­text = set­tings sleep(0.1)

Now our but­ton is ready to start the time­lapse se­quence. When the but­ton is held for two sec­onds, it calls the “cap­ture” func­tion (cre­ated ear­lier). This then cap­tures a time lapse us­ing the timer and de­lay vari­ables that the user will spec­ify.

but­ton.when_held = cap­ture

We now come to the fi­nal part of the project. Here we have the “ex­cept” part of the con­struc­tion. This will han­dle the user ex­it­ing the ap­pli­ca­tion by press­ing Ctrl+C, a key­board in­ter­rupt. It stops the pre­view and then moves on to the “fi­nally” part where we print that the ap­pli­ca­tion has ex­ited to the Python shell:

ex­cept Key­board­In­ter­rupt:

cam­era.stop_pre­view() fi­nally: print("TIME­LAPSE EX­IT­ING") Save the code and when ready click Run>Run Mod­ule to start. The pre­view win­dow will open and you can tweak the bright­ness and con­trast us­ing the po­ten­tiome­ters, set the timer and de­lay, then when ready press and hold the but­ton to start the se­quence. Now all you need to do is find a suitable sub­ject to pho­to­graph over time.

The cir­cuit has many parts. Fo­cus on one area and check your wiring be­fore mov­ing to the next bit.

The of­fi­cial Rasp­berry Pi cam­era has been de­signed for the Rasp­berry Pi and can cap­ture HD 1080p video and high-res­o­lu­tion im­ages.

Newspapers in English

Newspapers from Australia

© PressReader. All rights reserved.