Welcome back.
How was it?
Was it tough?
I hope you don't hate me for making it a little bit tough, but it's going to be a good thing.
And remember, if you didn't get it right away, that's not a big deal.
This is how we learn.
So let's make this a little bit bigger.
There you go.
And start off with our first step, which is to grab these two arguments that we're going to have to
use.
So how can we do this?
Well, first thing we're going to do is let's say path, which is the first path of our images.
Or let's just call it.
Image.
Folder.
And this image folder is going to use cis dot arc v which is the arguments and remember the arc v is
this is the first one which is the python file.
This is the second and this is the third.
The way programming works, we start with zero, then one, then two.
So what are we going to say?
Is our V one here?
And then for the second one, which is the output?
Folder.
It's going to be six dot arc v.
Two.
So I always like testing what we've done because we want to make sure that our assumptions are correct.
So let's just print image.
Folder and output folder.
And let's just run this file.
All right, look at that.
We get pokédex and new.
It looks like this is working.
Awesome.
So we did the first task.
What about the second one?
The second one is to check if this new folder exists.
If not, we need to create it so we can use it in here to put images.
So again, this sounds like an if statement.
So I'm going to say if.
And then check if the folder exists.
So the way we can do that is using the OSS module.
Now there's also the path lib module that you can use.
It's completely up to you.
Now with the odds.
What we can do is say o's dot path dot exists.
And this is just going to check, hey, does this exist?
So I'm going to say output folder.
Does this exist?
So let's just see what the output of this is.
So before I even do the if statement again, I like testing my code, so I'm going to say.
If O's path exists.
Or sorry.
Instead of if we want print, let's run this.
And we get false.
Why is it false?
Because it doesn't exist.
So in order for our if statement to work, we actually have to do this.
We have to say if.
Not because we want to get the opposite right.
So if this doesn't exist, then we want to convert this to true so that we can do something within this
block, which is to create the folder.
So how can we do that?
Once again, we use the OSS module and we use the make dirs or make directory method and we just give
it the directory output folder.
So again, let's test our code.
And if I run this.
Look at that.
We have the new folder.
Awesome.
And this way, if I run this code again, we see that we don't create another folder because, well,
we're just checking this.
And because this folder exists now, we're not going to duplicate and make another folder.
Awesome.
So step by step, we're making headway.
And again, you may be asking yourself, hold on, Andre.
I never learned about Make Ders.
How do I do that?
And again, this is something that you just have to practice where you look up what the model does and
you Google around and this is how you figure things out.
Nobody has things memorized as a programmer.
Don't hate me.
I'm just helping you practice that muscle.
All right, last part.
Last part is to loop through the pokédex, grab each image and convert it and save it to the new folder.
So how can we do that?
Well, we do a for loop and we'll say file name in and then again we'll use the OS dot list directory.
And this is going to be the items in the directory.
So with list dir, what we can do is just give it the image folder.
And in here we now have each of these file paths.
Now in here I can say image equals image dot open.
And what should we do here?
Because remember, we're going to open this every time, right?
And this file JPEG to PNG is outside of these folders.
So we somehow need to open.
These images at the pokédex.
But remember, we want to use whatever the input is because folder pokédex might not be always the same.
So instead of grabbing the Pokédex folder, we'll just simply use an SW string.
And within this string I'm going to say we want the.
Image.
Folder.
Which is going to give us the pokédex slash and the slash here, because we assume that the user going
to enter the slash, we don't have to include it in here and then give another string which will be
the file.
Name.
Which we grab from here.
So we now open these images, each one of them, and then we can just convert them, right?
So let's convert them by saying image, dot save.
And once again, we're going to use an F string and do the same thing, but this time for the output
folder.
So I'm going to say.
Output folder.
Give it the.
File name.
And then comma and then convert it to Pag.
And just for fun, let's just say print all done to let us know that every time an image gets processed,
we print all done.
Now we're going to try and run this code.
There's actually a bug in this code that we're going to have to fix, but let's just see what happens.
So I'm going to run this.
And there we go.
We have all done, all done, all done, all done.
Four images and it looks like it worked right.
Let's have a look.
If I go to the new folder here.
Hmm.
It looks like these are still in JPEGs.
That's strange.
Why would that be?
I thought we should be converting these.
Right.
Well, what if within here the save will do a dot, p and g?
So again, let's make this a little bit smaller.
So we'll say save us P&G in file format, P&G if I run this.
All right.
Look at that.
We now have the images, but.
Oh, we have.
Dot jpeg, dot pag.
What's going on here?
Well, we have to clean our name.
Right.
So what we need to do.
Is when we grab the image open.
We need to remove the dot jpeg from that image so we can save it as a p and g.
So how can we do that?
And that's something that you may have had to Google, because there's actually a useful tool that we
can use with the OS module that allows us to do this.
So what we can do is we can say clean.
Name and we'll say O's dot path.
Dot split text.
And what this is going to do is it's going to split the text for us.
So let me show you.
I'm going to say file name.
And in here, I'm not going to save anything.
I'm going to comment this out.
And I just want to print clean name.
So let's just see what split text does.
I'm going to clear this and run.
Again.
Oh, and I have an extra tea in here.
It should be split text.
So let's say that again.
And look at that.
It gives me a tuple that splits the text between the name and the extension.
That's extremely useful.
So we just need to keep this first part and we don't care about this part so we can just access the
clean name by saying zero.
So the first item in the tuple.
So now if I go back to this and instead of finally, we use the clean name to save the image.
This should work.
So let me remove some of the comments here.
And another thing I want to do is I want to remove all these images because we don't need them anymore.
We ideally have just P&G images, so I'm going to actually remove this new folder.
So let's say delete.
Okay.
You can delete it.
There you go.
I'm going to save.
And let's see if this works now.
Let's run our command.
All done.
If we go back.
Hey, check that out.
All our images are now PNG.
And you saw how fast that was.
We just created our first JPEG to PNG converter.
This file can now convert as long as you give it a folder with.
You can give it hundreds of images, thousands of images of JPEG and it's going to convert them to PNG.
If you want, you can even resize them.
You can even add image filters to them.
And we're able to do that all with a few lines of code of Python and some useful libraries.
Very, very cool.
Now we're just scratching the surface here, but you're starting to see the power of Python.
We have a problem.
And this problem can be solved with Python and its libraries.
And by just doing a bit of Googling, a little trial and error and debugging our code, we just made
something that could potentially save us a lot of time.
Instead of doing this manually.
Good job if you've gotten this far.
This is our first image processing project.
I hope you're excited.
There's still lots more to cover.
But give yourself a pat on the back and I'll see you in the next video.
Bye bye.