Welcome back.
I have a new problem for you.
I'm using a website here called Unsplash.com.
It's an awesome website where you can actually download for free.
Really good images.
Maybe you want to have a new background for your website, maybe a new background for your desktop.
Anyway, I found this spaceman image and I've downloaded it and I'll link to this image as well.
Or you can pick whatever image you want from Unsplash.com.
And you'll see here that.
This image is 3.8 megabytes.
It's massive.
And maybe I want to bring it down in size.
Maybe do a 400 by 400 thumbnail that I can use for my profile picture so people know that I'm an astronaut.
How can we do that?
Well, let's go back to our code.
We have this Astro Dot JPEG image and you can see over here, I'm scrolling through it.
It's it's absolutely massive.
So let's make this smaller.
I'm going to instead of pokédex say I want to grab the Astro JPEG image.
And we don't want to do any of this.
So I'm going to remove that.
And first of all, let's just print what the image sizes.
Just so we're clear.
So I'm going to run this and we see that.
Yep, it's a pretty big image.
So all I'm going to do is say new image is going to be image dot resize.
And again, a tuple that says 400 by 400.
And we've seen this before.
Right.
So now if I save this, let's say new image dot save.
And I'll call it.
Thumb nail dot.
JPEG.
If I save this and run this.
All right.
Look at that.
We have a thumbnail dot jpeg.
That's actually great.
Look at that.
It's tiny compared to the massive one we had.
But one thing that I don't know if you can notice is that because the image did not have the same width
and height.
What happened was it is actually a little bit compressed, so it lost its aspect ratio.
The image looks kind of squished in.
So how can we fix that?
And this is a common problem with images, right?
If we make things smaller and smaller or we change the aspect ratio, let's say.
400 by 200.
So if I save this and go to the image.
Well, yeah, look at that.
The image is all squished up.
How can we fix it?
Well, in this case we can actually use a useful thumbnail method.
So instead of saying resize, if you want to keep the aspect ratio, we can say thumbnail.
And this thumbnail will be 400 by 200.
So if I save and run this again, we'll get an error.
A non type object has no attribute safe and that is because a thumbnail actually doesn't return a new
image.
It just modifies the current one.
So in here we can just say image dot thumbnail.
And then we'll say image dot save.
So if I run this.
And we go back to thumbnail, look at that, we have a thumbnail, but it keeps the aspect ratio.
So even though I said 400 by 200, it's going to try and do its best to keep that aspect ratio and maybe
not give the exact output that I want.
So let's just bring it back to 400 by 400.
Run it again.
And there you go.
We have our thumbnail and the astronaut is not squished.
Very, very useful.
So the thumbnail one is really, really useful.
Maybe if you want to create a profile picture for Facebook or Instagram or any other social media platform
that you might have, and it just gives a tuple of width and height and this is the maximum value.
So it's never going to go over 404 hundred, but thumbnail is going to automatically do anything within
the range up to 400, but keeping the aspect ratio.
So if I actually do here print image dot size.
And I run this, you'll see that that's exactly what happens.
It's intelligently deciding what the best size for the image is.
All right.
Let's take a break.
And in the next video, we're going to do a fun little exercise.
I'll see you in that one.
Bye bye.