Great. So let's go ahead and play around
with OpenCV a little bit, and specifically,
what you'll learn in this lecture is
you will learn how to load images in
Python using OpenCV, and you will learn
how to display them, resize, and then
save the resize images in a new image files.
So load, display, resize, and
write images, and I've got a nice image
here of a galaxy. So I'll play around
with this. The first thing you want to
do is import library, and that the second
thing is you want to load the image in
Python. So image would be equal to
cv2.imageread method. So the
method expects now the path to the image
that you want to load in Python, and
that would be galaxy.jpg. So my
script1.py file is in the
same directory with galaxy.jpg,
so I just need to pass the file name
here, and then there is yet another parameter
for imageread, for imread
method, and then this parameter takes three
arguments. Now this parameter specifies
how you want to read the image in
Python. So do you want to read it as an
RBG image which means do you want three
bands in your image, so you want a color
image with a red band, the blue, and
green band, so if you want to read the
image as it is, so with colors, you'd
want to pass 1 here.
[No audio]
If you want to read the image as black and white
image in a grayscale, you'd want to pass
0, and having a grayscale image
implies that your image will have one
band, and I'll get back to bands and
explain them in just a moment. So and we
also have -1, and this means the
color image, but you also have an alpha
channel, which means your image will
have transparency capabilities. So if
you apply operations that require
transparency, you can do that when you
read, when you load the image with a
-1 argument here. Okay, so
I would like to try out 0.
[No audio]
Great. Now, before I show the image, before I
display the image on the screen, I'd
like you to understand what this image
object is about. So I'd want to print the type of it
[No audio]
just like that,
[No audio]
execute the script, script1. Yeah,
close and then try again.
[No audio]
So this is a NumPy n-dimensional array,
[No audio]
and if you want, you can print that out,
[No audio]
and you'll see the actual NumPy array.
[No audio]
So this is a 2-dimensional array with values in the
horizontal axis, and the vertical axis
as well. So think of the image now and
this would be the very first so the top
left intensity value of the first pixel.
So 14, for example, would be the
intensity value in the grayscale, for
the first pixel of the image, so for the
top left pixel of the image, and then
for the second pixel, and so on, and
then you have these dots, which means
Python cannot display the list, the
long list in here because you have a
couple of thousands values in the first
row, and then you have the second row of
pixels in the image, and the third, and so
on, and this maze of metrics of the
pixels of the image, and if you want to
know how many numbers, how many values
you have in the horizontal direction,
how many values you have in the vertical
directions, you can go ahead and print
image.shape. Okay. So let's say that the
image resolution is 1485 by 990.
So Python stores the image as a NumPy array,
as a matrix of numbers. As easy as that,
if you want to check the dimensionals of
your array of your image, you can do that
with this expression, and you see that
you have two dimensions. Now, if this
was a color image, so with three bands,
red, blue, and green,
[No audio]
things will change a little bit.
So you've got 3-dimensions,
[No audio]
and you also see that the
new array is a bit different. So here
you've got values for each of the bands.
So for green, for red, green, and blue.
So I'd like to stick with a great image.
And what I can do now is, I can display
the image on the screen, and for that, you
want to use the image. So imshow
method, and this will display a window
and you want to name that window. So you
want to put a title for that window,
let's say galaxy.
[No audio]
And what you pass here is
the image object. So this one, great,
and then what you want to do is
[No audio]
you want to specify a time for the audio
window to be closed. Because this will
show the window, but you also want to
define some functionalities, so that the
user can close the window. If you put 00
here, and while the user presses any
button, the window will close. Let me
change this cv2. So if you put
0 here, the user can close the window
pressing any button. If you want to put
a time, you could say 2000, and that
simplized 2000 milliseconds. So that
means 2 seconds. So we said how the
user wants to close the window, and then
you want to specify what to do when the
user presses a button or waits for 2
seconds. So he want to destroy all
windows. That's the method that closes
the window. Good. Let's see what
will happen. Okay, so we got the image slide,
and it waited for 2 seconds, and then it closed.
[No audio]
If you put it at 0,
[No audio]
the image will stay there, and if you
press a button, it will close, and let me
show the image again. Now, the reason
that you don't see the image fit on my
screen is that the image, as you see
here in these values is 1485 pixels
high. So the height, this is the
height, and it's 990 pixels wide.
So my screen resolution is set
at 1280 by 720. So that means this image
with this size does not fit on my
screen, because my screen is too small for this.
[No audio]
So in this case, let me close this.
What you can do is you want
to resize the image, and then show the
resized image. So we load the image,
and before showing it, before passing it
to the imshow method, you want to say
let's say resized
[No audio]
image that will be equal
to cv2.resize, and this would get
two parameters. So the first is, of
course, is the image object that you want
to resize. So img is our variable, and
then you want to specify a tuple with a
new dimensions, I'll say 1000 by 500.
And then you want to pass a new image here.
[No audio]
So what's happening here is that
Python is actually resizing the NumPy
array. So it will take the array with
this number of pixels, number of values.
and it will create an array with this
new dimensions. So what will happen
there is that Python will interpolate
those values. So it has quite a lot of
values here. But then it goes from this
to this. So when it sees let's say it
has 4 for a value and the 6
for the neighbor value, and what Python
will do is that it will get a 4 and 6
basically and it will just make a
value out of it, so let's say 5.
That's basically the idea. So it terminates
the values and then it shows
the interpolated image on the screen,
which looks pretty nice in our
eyes. Okay, let's see this,
[No audio]
and this is the image and here you see that this is quite
stretched a little bit. So it was a
total image but now it's quite wide.
And because this is actually the width
of the image and this is the height, so
if you want you can say 500 and 1000, so if
again, and now you see more or less the two
ratio of the image. But if you want
to keep the ratio of the image, you'd
want to go more advanced here. So let's
say from this, so let's say we would
want to show a size that is half of
this. So that that would keep the ratio
of the image. So what we can do is we
need access to these values, and we can
grab those values from the shape
method. So this produces a tuple with
these two values, and then we go here
and say, image.shape,
[No audio]
and this would be
this value, so 990, which has an index of 1,
and then we have again image.shape
index of 0 for this number,
[No audio]
and then we want to divide this by 2.
[No audio]
Okay, and I expects to get an
error from this. Let's see. So
yeah, we've got an error. It says TypeError
integer argument expected, got
float. But here, what we're doing here
is that when we divide this number by
2, we will get a float. So we would
get something like 742.5.
[No audio]
In that case, what we want to do is
convert this to an integer.
[No audio]
So 742.5 for becomes 742 .
Okay, and let's keep the consistency so
integers for these as well.
[No audio]
And let's see.
And we've got an invalid syntax here. So it
points us to the line 11, somewhere in the
beginning, which can be quite
misleading. So you want to see before
that line, which is here, and you can
see that these brackets here closes
here, so we need another bracket, which
closes in the first bracket here. So
save that, try again.
[No audio]
And this time the image looks good.
So you will learn how
to load an image in Python, and you'll
learn how to resize an image, how to show
an image on the screen, and now let's go
ahead and write the resized_image in
a new file. For that, you'd want to use
the imwrite methods. So imagewrite
and you want to give a name to
the new image Galaxy, let's say, resized.jpg,
[No audio]
and then you pass the image
object that you want to store in this
file. So the comma goes also here,
and the image you want to store is
resized_image. That's it.
[No audio]
Execute, and we got
the old image resize on the fly. So
Python gets a NumPy array, it
interpolates it, so it resizes it and then
it shows it on the screen. So this is
galaxy, this one here, and then we can close
this, and here we've got our new image.
So Galaxy_resized.
[No audio]
So if you go to the
folder where these files are, you'll see
that this image has new dimensions. So
495 by 742, and that's what I wanted to
teach you in this lecture. Hope you
enjoyed it and talk to you later.