First open the python editor, Thonny.
This is what the editor looks like.
Let's start coding! First, import the packages we will use.
Next, set up the camera. Also called, instantiating the camera.
Now, write the code for taking the photo.
Before testing the code, it needs to be saved. Save the file in the folder where you want your image to be saved.
Click the run button to start the program.
Once your code is finished running, the new image will be saved in your folder!
Now, let's add a little more code to create a time lapse video.
Here we are adding a for loop which repeats the code indented underneath it as many times as you tell it. The range (0, 15) will run this code 15 times. However, we also need to change the file name of the image or else the image will be overwritten 15 times! Here I am adding the current value of i to the end of the filename. The value of i will move through the range during the for loop.
After running the new code (saved as a new python file in a different folder) you can see the Pi has taken a series of images all named in sequential order. The numbers start with 0 and end with 14 which is a total of 15 images.
The images are great, but it would be even better if python stitched them all together into a gif!
To do this, we will use the package, Image Magick. However, there are many options out there.
You may need to install Image Magick, simply type "sudo apt-get install imagemagick -y" in the Pi terminal.
Once you have installed the package, write in the code to import it into your python program.
After the end of the for loop, add the code to stitch together the images. This can take a little while so I am adding a print statement below so that I know when it is finished.
After running the code, you can see the print statement in the shell at the bottom of Thonny.
Go to your folder and view your timelapse!
Next, you can modify the code by setting the time lapse to start after pressing a button, to run only during specific hours, or to tweet you the pictures! The options are really endless. I'd recommend picking up The Official Raspberry Pi Camera book. You can download it for free and see tons of great projects along with the full code and an explanation of what the code does. You can make an underwater camera, an infrared bird box camera, a wildlife camera, and so much more!
You can also use computer vision on the Pi for more advanced image detection. A wonderful computer vision library is OpenCV. It can be a bit of a trick to install so follow the next page of the libguide for instructions.
Happy coding!