In this video, I'll create a script which converts color images, multiple color images into grayscale.
So this is a solution to the exercise that you just got in the previous article.
So this is my rifle.
I have this empty main.
That's why file here.
And then I have prepared two folders, great images here and images images contains these three files.
So three JPEG color images.
So the purpose now is to convert these into grayscale and place them inside great images with Python,
of course.
Let's do that.
What can we do first?
How do you approach this problem?
So when we perform multiple operations, we think about four loops.
Therefore, it makes sense to create some sort of four.
But what do we iterate through?
We should iterate these file paths right away and do something with each of those file paths.
Therefore, we need a list of those for pass.
So I'm going to create a list using the OSU library, even though you could also use the Globe Library.
I prefer OS
and the list of methods of OS, which expects a path to a folder that contains the files for which you
want to create a list for.
So images that one is the path.
And if a print images now and run, we get a list.
It's a Python list and its image has its file name actually is the file path relative to the images
folder.
So the file path for Bear is there, the JPEG galaxy, the JPEG and so on.
Now we can iterate for image in images.
What should we do to each image?
Well, first we want to open needs an open CV.
For that, we need to import CV to.
And then we say here, Kavita, to Dutt, I am reads.
So we should provide the file name, right?
Therefore, we see image, but this is not enough.
You'll see why.
Anyway, let's leave it for now and flag.
We said, that's who we warned Grayscale Images.
So let's give this as a grayscale and store it in a gray variable.
So this variable contains the image object in grayscale mode.
So once you create this great image, you want to run, I am right, which gets a file name.
So this is the file name for the new image.
And if you just give it a file name such as F, I saw an f string and you could do something like gray
dash.
And then you put the variable image, which in the current loop contains the bear, the JPEG image in
the next, it will contain galaxy that JPEG and so on.
So the file name will be Gray Dash, etc. That's a JPEG.
Right?
Let's run and see what errors we get.
So first, we need to provide that image, of course.
But even though we provide the object, the image object will gets another error, which is a bit difficult
to understand, actually.
So it sees that image empty in function.
I'm right, which means that this thing here gray is empty.
We could verify that by printing gray out.
So print gray and you'll see it's none.
That is because we have an issue here with paths.
So when we do for image in images, we do get the image correctly as you see so bear.
Was printed out here, and then we got none for Gray.
The gray version of beer.
So something here is not working well and that something is because, you know, if this mean that's
why file is in this project directory here.
And that means that if you give the script a file name such as Bear the J Pack as in this case.
This script will look for Baird, the Jeep back in the main directory.
So in the same directory where the script is, so it will look here, but there is no bare JPEG file
in this directory.
There is a bear, the Jeep, but that is inside.
Images slash Baird, the Jeep back.
So now it should work.
So we get grey printed out the matrix and then we get printed out galaxy the jet pack for the next iteration.
For that, the Galaxy Matrix and then Python, the jetpack and the Python matrix for that image.
You see this to 55 to 55 to 55.
And these are the pixels for the whites.
Once the white pixels, which means you have 255 amount of reds and 255 amount of green and 255 amounts
of blue, which gives a white pixel, that's sort of combination.
Now we still have another issue here.
You see that the grayscale images were not generated inside gallery images, but inside this main directory
because of course, here when we grow them again, we just declare the file name.
So since the script is in these main directory, it will just write a file in that main directory.
Therefore, here we could also do grey images slash that.
So now we give a string, which is a complete file path, including the folder.
So if you see in gray images now, after I ran the script, you see the grayscale versions of those
images.
So I don't want these JPEG images here, so I'm just going to delete them.
You can delete them here one by one, or you can go to the shell and to our EM four, remove all and
everything for it.
So asterisk means everything that JPEG wrong, that those images will disappear.
But no, the was inside the folders because here we are in the main directory in that folder.
So all that JPEG images of that folder were were deleted.
And that is how to convert multiple images to Grayscale.
Thank you for.
I'll talk to you later.