ZacharyIgielman
Posts: 101
Joined: Sun Dec 08, 2013 11:27 am
Location: London

How to do time lapses with the Raspberry Pi

Mon Jan 20, 2014 6:11 pm

This post is structured like this:

1. How to do a time lapse with the Raspberry Pi
1. Taking the pictures (in light or dark)
2. Compressing the pictures
3. Doing it all in one script
2. My time-lapses
1. Day-time time-lapse of my Guinea Pigs
2. Night-time time-lapse of my Guinea Pigs
3. In car battery powered time-lapse
3. Jam-lapse

What is a time-lapse?

Time-lapse is where a camera takes a sequence of images of a subject with an interval of time between each image. The interval can be anything from less than a second to a day or more. When the images are played back the interval of time is speeded up creating shorter time.

How to do a time lapse with the Raspberry Pi and camera module

1. Taking the photos:

There are two main ways to take a certain number of photos at a certain interval with a Raspberry Pi. Obviosly you need a recent version of Raspbian, and you need either camera module connected to the camera connector correctly. If you want to do this in the dark, you will have to use the Pi NoIR camera module with an infrared light source.

Option 1:
You could create a python script or a shell script that runs the raspistill command in a while loop. Here is an example python script that takes 1000 photos 6 seconds apart, with the names counting up from image0000000.jpg to image0000999.

Code: Select all

import os
import time

FRAMES = 1000
TIMEBETWEEN = 6

frameCount = 0
while frameCount < FRAMES:
    imageNumber = str(frameCount).zfill(7)
    os.system("raspistill -o image%s.jpg"%(imageNumber))
    frameCount += 1
    time.sleep(TIMEBETWEEN - 6) #Takes roughly 6 seconds to take a picture
Option 2:
You can also use the time-lapse command built in to the raspistill API which is very convenient and easy, which we are very lucky to have (thanks Raspberry Pi!). You type one command with a few values that you specify depending on how long you want it to last and how far apart you want them to be taking in milliseconds into a terminal and it takes the photos. Eg:

Code: Select all

raspistill -t 30000 -tl 2000 -o image%04d.jpg
will produce a capture every 2 seconds over a total period of 30s, named image1.jpg, image0002.jpg...image0015.jpg. Note that the %04d indicates a four-digit number with leading zeros added to pad to the required number of digits. So, for example, %08d would result in an eight-digit number.

Compressing the photos into a video:

There are two commands that I use to compress my photos into a time-lapse video.

This is the first command:

Code: Select all

ls *.jpg > stills.txt
It simply lists all the jpeg files in the current directory into a text file called stills.txt in order of their name (make sure to run it in the directory of the time-lapse photos).

This is the second command:

Code: Select all

mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=1920:1080 -o tlcam.avi -mf type=jpeg:fps=24 mf://@stills.txt
It uses a piece of software called mencoder (which you have to install with this command: ‘sudo apt-get install mencoder’) to compress the pictures listed in the stills.txt (in the order that they are listed) into a video file called tlcam.avi. Make sure to run it in the same directory as the stills.txt file. The value in bold (in our case 24) is the frames displayed per second in the video. This means that it plays 24 of our photos taken per second.

Doing it all in one script

You could put these commands that compress the photos into video, at the end of a python or shell script that takes the photos so that you only have to run one script that takes the photos and compresses them. Here is example python script that takes the photos and compresses them:

Code: Select all

import os
import time

FRAMES = 1000
TIMEBETWEEN = 6

frameCount = 0
while frameCount < FRAMES:
    imageNumber = str(frameCount).zfill(7)
    os.system("raspistill -o image%s.jpg"%(imageNumber))
    frameCount += 1
    time.sleep(TIMEBETWEEN - 6) #Takes roughly 6 seconds to[url][/url] take a picture

os.system(ls *.jpg > stills.txt)
os.system(mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=1920:1080 -o tlcam.avi -mf type=jpeg:fps=24 mf://@stills.txt)
My time lapses:

Daytime Guinea Pig Time Lapse: http://www.youtube.com/watch?v=yzTJ3xnY5sk

This was pretty simple. I have plenty of plug sockets in my garage, so I brought across my TV, a lamp and a micro USB 5v power supply and plugged them in. I hooked a pi up to an SD card with the latest raspbian, a normal raspberry pi camera module with the camera connector, the TV via HDMI, a keyboard via USB and the power supply. I positioned the camera module so it could see most of the enclosure (to see a constant live preview from cam module on screen, type: 'raspivid -t 0') with a mount and some tape. I then bashed in the commands spoken about previously.

Nighttime Guinea Pig Time Lapse: http://www.youtube.com/watch?v=dkheeVw_3Fc

I did this the same as above, but I did not want to keep my Guinea Pigs awake with light (specifically light that they can see). Fortunately, they make a version of the raspberry pi camera that doesn't filter out IR light. I bought this, and as it works exactly the same as the normal module, I used the same method as previously, but I used a 48 LED IR CCTV ILLUMINATOR from amazon or ebay instead of the lamp as it emits light that the Pi NoIR can see, but the Guinea Pigs can't.

In Car Time Lapse: http://www.youtube.com/watch?v=p8RxZKCX2DY

This was a bit of a nightmare. As I do not have a plug sockets in the car, I had to battery power the pi. I used a 5v USB battery pack phone charger to power the Pi. As I could not have any sort of screen in the car, I entered the commands blindly. I powered the pi (with a keyboard and camera plugged in), waited a minute or so, entered my username, entered my password, then entered the first command (raspistill) to take the photos. I could tell that I had done this correctly as the camera light flashed every 6 seconds (I wanted a photo to be taken every six seconds). After the journey, I just took the power supply away from the Pi. This was very risky, and could've ruined my sd card. Luckily it didn't, and when I was home, a hooked it up with my TV and compressed the photos.
Last edited by ZacharyIgielman on Mon May 25, 2015 8:23 am, edited 1 time in total.
Don't be mean, I'm only fifteen :D

User avatar
DougieLawson
Posts: 37095
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: How to do time lapses with the Raspberry Pi

Tue Jan 21, 2014 11:41 pm

Have you thought about running the pi from the cigarette lighter socket in your car?
http://www.amazon.co.uk/gp/product/B001T9N4GS/ claims to give 5V @ 1A.

Also why not code the start up for raspistill in /etc/rc.local or in a /etc/init.d script so you don't have to log on (blind) to get it started.

My RPi has been watching the birdtable (one shot every minute) since the tail end of December.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

TrevorAppleton
Posts: 74
Joined: Wed May 30, 2012 7:26 pm
Contact: Website

Re: How to do time lapses with the Raspberry Pi

Tue Feb 11, 2014 1:14 pm

If anyone would like to see the original tutorial where the python code for this tutorial has been copied from please have a look at my blog post.

http://www.trevorappleton.blogspot.co.u ... -with.html
Check out my blog post for Raspberry Pi and Python tutorials.

http://trevorappleton.blogspot.co.uk/

ZacharyIgielman
Posts: 101
Joined: Sun Dec 08, 2013 11:27 am
Location: London

Re: How to do time lapses with the Raspberry Pi

Tue Feb 11, 2014 3:52 pm

TrevorAppleton wrote:If anyone would like to see the original tutorial where the python code for this tutorial has been copied from please have a look at my blog post.

http://www.trevorappleton.blogspot.co.u ... -with.html
Sorry. When I was writing I couldn't find the link. That's the one, thanks!
Don't be mean, I'm only fifteen :D

kyle2k
Posts: 84
Joined: Tue Feb 19, 2013 2:47 pm

Re: How to do time lapses with the Raspberry Pi

Tue Feb 11, 2014 6:45 pm

Hi,

I'm just learning Python myself so no expert.
From what I have been reading so far it seems that you should now be using subprocesses.call() instead of os.system() to run the raspistill command.

http://www.raspberrypi.org/phpBB3/viewt ... &p=423660#

speedyroadster
Posts: 15
Joined: Wed Aug 29, 2012 2:56 pm

Re: How to do time lapses with the Raspberry Pi

Mon Mar 24, 2014 1:00 pm

Hi,
I've used the code from 'option 1' which works perfectly. I just wondered if it possible for the image files created to be saved in a new folder, or should I create a new folder, save the code in there and run it from that location? I am a bit of a newbie but found the Geek Girl video and your code very helpful.

Many thanks,
Alan

ZacharyIgielman
Posts: 101
Joined: Sun Dec 08, 2013 11:27 am
Location: London

Re: How to do time lapses with the Raspberry Pi

Mon Mar 24, 2014 6:25 pm

speedyroadster wrote:Hi,
I've used the code from 'option 1' which works perfectly. I just wondered if it possible for the image files created to be saved in a new folder, or should I create a new folder, save the code in there and run it from that location? I am a bit of a newbie but found the Geek Girl video and your code very helpful.

Many thanks,
Alan
change the 'os.system(raspistill)' line within the while loop to:

Code: Select all

os.system("raspistill -o ./newFolder/image%s.jpg"%(imageNumber))
This should save it to a folder called 'newFolder' within the directory of the script. If you want the program to automatically make this 'newFolder' folder then add this command before the while:

Code: Select all

os.system("mkdir ./newFolder")
Don't be mean, I'm only fifteen :D

speedyroadster
Posts: 15
Joined: Wed Aug 29, 2012 2:56 pm

Re: How to do time lapses with the Raspberry Pi

Mon Mar 24, 2014 8:58 pm

Many thanks, I'll give it a try :-)

Alan

ZacharyIgielman
Posts: 101
Joined: Sun Dec 08, 2013 11:27 am
Location: London

Re: How to do time lapses with the Raspberry Pi

Mon Mar 24, 2014 9:10 pm

speedyroadster wrote:Many thanks, I'll give it a try :-)

Alan
tell me how it goes, happy to give more help!
Don't be mean, I'm only fifteen :D

speedyroadster
Posts: 15
Joined: Wed Aug 29, 2012 2:56 pm

Re: How to do time lapses with the Raspberry Pi

Fri Mar 28, 2014 6:27 pm

Brilliant, the code to make a folder and save the images to that folder works a treat.
Probably getting a bit carried away but I thought if the program asked for a folder name I could then organise them better and wouldn't get over-written.

so I started like this:

foldername = input("Enter name for picture folder ") # not sure if it should be str input ??

os.system("mkdir ./(foldername)")

In the 'while' loop:

os.system("raspistill -o ./(foldername)/image%s.jpg"%(imagenumber))

I know this doesn't work, my Pi said so :lol:

Please can you let me know where I've gone wrong.

Many thanks,
Alan

ZacharyIgielman
Posts: 101
Joined: Sun Dec 08, 2013 11:27 am
Location: London

Re: How to do time lapses with the Raspberry Pi

Fri Mar 28, 2014 9:30 pm

speedyroadster wrote:Brilliant, the code to make a folder and save the images to that folder works a treat.
Probably getting a bit carried away but I thought if the program asked for a folder name I could then organise them better and wouldn't get over-written.

so I started like this:

foldername = input("Enter name for picture folder ") # not sure if it should be str input ??

os.system("mkdir ./(foldername)")

In the 'while' loop:

os.system("raspistill -o ./(foldername)/image%s.jpg"%(imagenumber))

I know this doesn't work, my Pi said so :lol:

Please can you let me know where I've gone wrong.

Many thanks,
Alan
I just knocked this up (do ask if you don't understand it or if it does't work):

Code: Select all

import os
import time

foldername = input("Enter name for picture folder ")

os.system("mkdir ./"+foldername)

FRAMES = 1000
TIMEBETWEEN = 6

frameCount = 0
while frameCount < FRAMES:
    imageNumber = str(frameCount).zfill(7)
    os.system("raspistill -o ./"+foldername+"/image%s.jpg"%(imagenumber))
    frameCount += 1
    time.sleep(TIMEBETWEEN - 6) #Takes roughly 6 seconds to[url][/url] take a picture

os.system("ls ./"+foldername+"/*.jpg" > stills.txt)
os.system("mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=1920:1080 -o tlcam.avi -mf type=jpeg:fps=24 mf://@stills.txt")
Don't be mean, I'm only fifteen :D

speedyroadster
Posts: 15
Joined: Wed Aug 29, 2012 2:56 pm

Re: How to do time lapses with the Raspberry Pi

Sat Mar 29, 2014 3:15 pm

This is what I have tried:

# timelapse4.py

import time
import os

duration = float(input("How many hours do you want to capture? "))

timebetween = float(input("How many seconds do you want between pictures? min 6 seconds "))

frames = int((duration * 3600)/timebetween)

print "PiCam will create ",frames," pictures"
print("Make sure there is enough space on the SD memory card")

foldername = (input("Please enter a name for the file folder ")
print("Pictures will be saved into a folder called ", foldername)


delaystart = float(input("How many hours do you want to delay the start? "))
print (delaystart * 3600)," seconds until photos captured"
time.sleep(delaystart * 3600)

framecount = 0

os.system("mkdir ./"+foldername)

while framecount<frames:
imagenumber = str(framecount).zfill(7)
os.system("raspistill -o ./"+foldername+"/image%s.jpg"%(imagenumber))
framecount +=1
time.sleep(timebetween -6)

When I run this my Pi says:


[email protected] ~ $ sudo python timelapse4.py
How many hours do you want to capture? .1
How many seconds do you want between pictures? min 6 seconds 10
PiCam will create 36 pictures
Make sure there is enough space on the SD memory card
Please enter a name for the file folder newfolder2
Traceback (most recent call last):
File "timelapse4.py", line 15, in <module>
foldername = str(input("Please enter a name for the file folder "))
File "<string>", line 1, in <module>
NameError: name 'newfolder2' is not defined

????????????? :?
Any help, greatly received,
Alan

ZacharyIgielman
Posts: 101
Joined: Sun Dec 08, 2013 11:27 am
Location: London

Re: How to do time lapses with the Raspberry Pi

Sat Mar 29, 2014 3:25 pm

speedyroadster wrote:This is what I have tried:

# timelapse4.py

import time
import os

duration = float(input("How many hours do you want to capture? "))

timebetween = float(input("How many seconds do you want between pictures? min 6 seconds "))

frames = int((duration * 3600)/timebetween)

print "PiCam will create ",frames," pictures"
print("Make sure there is enough space on the SD memory card")

foldername = (input("Please enter a name for the file folder ")
print("Pictures will be saved into a folder called ", foldername)


delaystart = float(input("How many hours do you want to delay the start? "))
print (delaystart * 3600)," seconds until photos captured"
time.sleep(delaystart * 3600)

framecount = 0

os.system("mkdir ./"+foldername)

while framecount<frames:
imagenumber = str(framecount).zfill(7)
os.system("raspistill -o ./"+foldername+"/image%s.jpg"%(imagenumber))
framecount +=1
time.sleep(timebetween -6)

When I run this my Pi says:


[email protected] ~ $ sudo python timelapse4.py
How many hours do you want to capture? .1
How many seconds do you want between pictures? min 6 seconds 10
PiCam will create 36 pictures
Make sure there is enough space on the SD memory card
Please enter a name for the file folder newfolder2
Traceback (most recent call last):
File "timelapse4.py", line 15, in <module>
foldername = str(input("Please enter a name for the file folder "))
File "<string>", line 1, in <module>
NameError: name 'newfolder2' is not defined

????????????? :?
Any help, greatly received,
Alan
The line with the error in your code looks like this:

Code: Select all

foldername = (input("Please enter a name for the file folder ")
You have not got str before the first bracket, so the bracket is pointless. You have two open brackets and only one close. Try making sure the line that the error is in is perfect! :)
Don't be mean, I'm only fifteen :D

speedyroadster
Posts: 15
Joined: Wed Aug 29, 2012 2:56 pm

Re: How to do time lapses with the Raspberry Pi

Sat Mar 29, 2014 7:39 pm

All sorted!! Sorry feel a bit of a muppet about the brackets!

foldername = str(input("Please enter a name for the file folder "))

Also, from the terminal I was typing:
sudo python timelapse4.py

When I try:
sudo python3 timelapse4.py
(i thought it would automatically default to v3?)

everything works! :D
Off to set it up to capture the sun rise tomorrow - I hope!
Thank you for your help, Alan

ZacharyIgielman
Posts: 101
Joined: Sun Dec 08, 2013 11:27 am
Location: London

Re: How to do time lapses with the Raspberry Pi

Sun Mar 30, 2014 6:49 am

speedyroadster wrote:All sorted!! Sorry feel a bit of a muppet about the brackets!

foldername = str(input("Please enter a name for the file folder "))

Also, from the terminal I was typing:
sudo python timelapse4.py

When I try:
sudo python3 timelapse4.py
(i thought it would automatically default to v3?)

everything works! :D
Off to set it up to capture the sun rise tomorrow - I hope!
Thank you for your help, Alan
Great! Keep me updates and show me what you take :D
Don't be mean, I'm only fifteen :D

speedyroadster
Posts: 15
Joined: Wed Aug 29, 2012 2:56 pm

Re: How to do time lapses with the Raspberry Pi

Thu Apr 10, 2014 6:08 pm

Still waiting for a good sun rise - something to do with sand pollution in the sky or some other annoyance!

Anyway here is the code that I am using which seems fairly usable.

# timelapse5.py

import time
import os

duration = float(input("How many hours do you want to capture? "))

timebetween = float(input("How many seconds do you want between pictures? min 6 seconds "))

frames = int((duration * 3600)/timebetween)

print("PiCam will create ",frames," pictures")
print("Make sure there is enough space on the SD memory card")

foldername = str(input("Please enter a name for the file folder "))
print("Pictures will be saved into a folder called ", foldername)

prefix = str(input("please enter a prefix name for pictures "))
print("Pictures will have ",prefix," as a prefix")

delaystart = float(input("How many hours do you want to delay the start? "))
print ((delaystart * 3600),(" seconds until photos captured"))
time.sleep(delaystart * 3600)

framecount = 0

os.system("mkdir ./"+foldername)

while framecount<frames:
imagenumber = str(framecount).zfill(7)
os.system("raspistill -o ./"+foldername+"/"+prefix+"%s.jpg"%(imagenumber))
framecount +=1
time.sleep(timebetween -6)

Regards,
Alan

nitschkecm
Posts: 50
Joined: Wed Dec 17, 2014 11:01 am

Re: How to do time lapses with the Raspberry Pi

Sun Jan 11, 2015 7:48 pm

Sorry for a total noob question:

I am trying to do this

Code: Select all

import os
os.system("ls *.jpg" > stills.txt)
but I am getting this error
Traceback (most recent call last):
File "file.py", line 2, in <module>
os.system("ls *.jpg" > stills.txt)
NameError: name 'stills' is not defined
What am I doing wrong. This is basically a copy paste of what I have seen above...
Thanks and sorry for the stupid question!

ZacharyIgielman
Posts: 101
Joined: Sun Dec 08, 2013 11:27 am
Location: London

Re: How to do time lapses with the Raspberry Pi

Sun Jan 11, 2015 7:55 pm

nitschkecm wrote:Sorry for a total noob question:

I am trying to do this

Code: Select all

import os
os.system("ls *.jpg" > stills.txt)
but I am getting this error
Traceback (most recent call last):
File "file.py", line 2, in <module>
os.system("ls *.jpg" > stills.txt)
NameError: name 'stills' is not defined
What am I doing wrong. This is basically a copy paste of what I have seen above...
Thanks and sorry for the stupid question!
Slight syntax error. It should be:

Code: Select all

import os
os.system("ls *.jpg" > stills.txt")
You want it to run everything between the ""s in the terminal.

Try that and let me know how it goes :)
Don't be mean, I'm only fifteen :D

nitschkecm
Posts: 50
Joined: Wed Dec 17, 2014 11:01 am

Re: How to do time lapses with the Raspberry Pi

Sun Jan 11, 2015 8:02 pm

Hi Thanks !
actually you are right it must be all between the ""!!! I believe you have a small typo in your reply. Tried it out and finally works!

paesrn
Posts: 1
Joined: Sun Mar 24, 2019 5:40 pm

Re: How to do time lapses with the Raspberry Pi

Sun Mar 24, 2019 5:42 pm

how can i stop the while loop during the processing just break it like if we have got sth wrong in the pic and we would like to look on it so how could i do

appreciate

Return to “Camera board”