In this lesson you will learn how to take a photo with Python using the Raspberry Pi camera.
So how can we take a photo?
Well, here again we are going to use a Python module.
And which module will we use?
Well the name is going to be pi cam zero.
So we have the Gpio zero for the Gpio and we have the Pi cam zero for the camera okay.
It's quite easy to use library.
Now this one is not installed by default.
So we will need to install it.
Let's open a terminal.
And we are going to try to install it with apt first.
So the first thing I need to do I'm going to do a sudo apt and update to update all the sources.
So I have the latest available versions.
All right.
And well you can see already I have 81 packages that can be upgraded.
So what I'm going to install the package now.
But as best practice if you see this you need to upgrade the packages with sudo apt upgrade.
Okay I'm just going to do that later.
If you have this, you can do that first.
So let's do a sudo apt install and then python3 dash and let's type pi cam zero.
Actually let's press tab here.
And you can see it is found.
So we can install this library with apt.
Let's run.
So let's press enter.
And you can see that might be a lot of packages actually.
So it's gonna download 50MB and add about 250MB of space.
And here for me it's going to install 65 new packages.
So it's quite a lot.
Let's press enter.
And let's wait for the installation to finish.
And the package is successfully installed.
So it took a few minutes for me but now it is done.
So let's just test that we can use actually that we can import this library.
So you open a Python shell from from here or even from the terminal.
So Python three let's do import and then become zero.
And you see, well, we don't get an error, which means that it is correctly installed and we can use
it.
So exit okay.
And let's go to Tony.
So how to use that library.
Well we will do from Pi zero import and then camera with an uppercase camera like this.
And then you can import a camera with.
So let's create a camera variable camera is equal to camera.
And then open and close parentheses.
If you remember how we initialized an LED or a pushbutton it's very similar okay.
You import the functionality from the library and then you just create a variable and you put the name
with open close parentheses.
Here we don't need to provide anything.
And just a quick note is that here again we do from module import functionality.
Let's say we just did Import pi can zero and let's comment that line.
So if I did just that then I would need to do pi zero dot camera.
So it's the same.
Okay I import the whole module and then I do module dot functionality.
So you can do it like this.
Or you can just import the functionality that we want.
And that's what I'm going to do now.
So I do from Pi Zero import camera.
And then I can use camera directly.
All right.
So this is going to initialize the camera.
Now if you remember we did two things on the terminal.
We changed the resolution.
And we also did a vertical flip and a horizontal flip.
We will need to do the same thing here.
So let's do camera.
And we have an option.
It's called steal size okay.
So steal for the photo like you had the steal comment on the terminal steal Still size and this one
you need to do equal and give a what's called a tuple.
I'm going to write 1536 and 864.
That's the resolution we want.
All right.
So you write a tuple like this with parentheses.
And then each item is separated by a comma.
So we haven't seen tuples yet but very basically put a tuple is the same as a list.
The only difference is that well you can modify elements in the list and you can add for example elements.
But with a tuple you cannot do that.
Okay.
So tuple is created and after it's created you cannot modify it.
That's the difference.
And instead of brackets we have parentheses.
That's it.
And why we write a tuple.
Well simply because this option is expecting a tuple.
So we need to give a tuple.
So we have camera dot still size is equal to tuple.
Then we can do camera dot flip camera.
And this one is actually not an option.
This one is a function.
Okay.
So we actually are calling a function and you need to do so we can have h flip.
We're going to provide h flip argument and put it to true.
So is equal true.
And then comma and then v flip is equal to true.
And close the parentheses.
All right.
So we call the function flip camera with those two arguments h flip and v flip.
You can put only one of those if you want.
Here we need the two.
Great.
And then what we're going to do is that after the camera is initialized I'm going to add.
So I'm going to import time.
And I'm going to add a time dot sleep for two seconds.
So just once after we initialize the camera I'm just giving two seconds for the camera to be fully initialized.
And maybe the autofocus to work correctly just so that we have a good quality image, and after that,
then we can take a photo.
All right.
So that's the initialization of the camera.
So it's not really too complicated.
You create a camera here and then you set for example the resolution.
You flip the camera, you wait for a few seconds and then you can do whatever you want.
So to take a photo you have camera dot and you have the take photo function.
Okay.
So quite easy.
And the parameter we need to give here is the path for the file to save the photo.
So we want to go in this.
Well still in this camera folder here.
And you can see the path here.
We're going to put the absolute path is slash home slash pi slash camera.
Let's write this slash home slash API slash camera slash and let's call it my photo dot jpg.
So I close everything all right.
And well let's just add a print.
Um, done.
Just so we know that the photo has been taken, because at least we have something printed on the terminal.
Great.
So the only thing left to do is to run this program, actually, so we can do it from here or from the
terminal.
Let's run it here.
Okay.
So we wait a bit.
It's going to initialize the camera and wait for two seconds.
And then you see done.
And after it's done we can see our photo here.
My photo.
And that's what.
That's a photo that's been taken from the Python script.
As you can see here, we didn't have a preview.
So that's something you could enable if you want.
But we don't really need that from a Python script.
Okay.
The preview was available by default on the terminal, so it's quite nice.
Also, you can correctly position the camera and make some tests, but if you are working from a Python
script and you're going to integrate the camera functionality in a bigger project, you don't necessarily
want to have a preview every time, okay, you know that the camera is set up, so you just initialize
it once and then you just take a photo like that, it's going to be much more efficient.