1
00:00:00,390 --> 00:00:06,390
In this video, we're going to write a script which criticizes this JPEG image.

2
00:00:06,510 --> 00:00:12,480
It will make it smaller, given a scale percentage that we can give to Python.

3
00:00:13,350 --> 00:00:22,540
So to do this, we need to import CV to and start by loading the image into an image open.

4
00:00:22,560 --> 00:00:30,060
See the object which goes like this if it's who I am or reads Galaxy Dot JPEG.

5
00:00:30,330 --> 00:00:40,050
And if you print out the image that shape, you're going to see the shape of the image, which means

6
00:00:40,050 --> 00:00:49,170
that this first number here is the height of the image because you see that this image is sort of vertical

7
00:00:49,320 --> 00:00:50,340
portrait mode.

8
00:00:51,120 --> 00:00:58,230
So this is the height here represented by that, and the width is the second number.

9
00:00:58,350 --> 00:01:05,129
And three means there are three mattresses with this height and width.

10
00:01:05,430 --> 00:01:10,080
And each mattress has different values, different pixels.

11
00:01:10,680 --> 00:01:13,590
So for red, green and blue.

12
00:01:13,590 --> 00:01:19,950
So every pixel of the image is represented by a mixture of red, green and blue, as we have discussed

13
00:01:19,950 --> 00:01:20,580
previously.

14
00:01:20,640 --> 00:01:29,400
However, the colors are not of any interest for us because we are only interested about the size of

15
00:01:29,400 --> 00:01:30,540
this image for now.

16
00:01:30,930 --> 00:01:35,960
So that is how you get the dimensions and you want to calculate the new dimensions.

17
00:01:36,020 --> 00:01:43,200
Now, right now, when we're talking about calculations, it makes more sense to have a function defined

18
00:01:43,860 --> 00:01:45,990
calculate light size.

19
00:01:46,650 --> 00:01:57,210
So which gets a scale percentage as inputs, and it also gets a width and the height, perhaps.

20
00:01:57,480 --> 00:02:07,590
And then it calculates the new width equal to width times scale percentage.

21
00:02:08,250 --> 00:02:14,670
So with scale percentage, what we expect to have here is something like an integer or floats.

22
00:02:14,940 --> 00:02:22,980
So, for example, 10, which means make the new image 10 percent of the size of the old image.

23
00:02:23,280 --> 00:02:27,660
So we should say, for example, let's say the original height was seven seven nine.

24
00:02:27,960 --> 00:02:33,060
So we would multiply by 10 and divide by 100.

25
00:02:33,390 --> 00:02:35,550
And we get the new dimension.

26
00:02:35,790 --> 00:02:40,800
That's what I'm doing here with times scale percentage divided by hundreds.

27
00:02:41,310 --> 00:02:46,890
I'm going to copy that paste here, and this is for new height.

28
00:02:49,970 --> 00:02:53,720
Eager to highlight the scale percentage is the same.

29
00:02:53,990 --> 00:03:05,200
And so one very soon I would return a tuple here with new width and new height rates.

30
00:03:06,080 --> 00:03:13,910
We have an underline here, which means, yeah, undefined name width because I mistyped it's here w

31
00:03:14,060 --> 00:03:15,890
d, which it should be.

32
00:03:16,340 --> 00:03:17,480
Yeah, now it's fine.

33
00:03:18,050 --> 00:03:23,450
So let's test this function first to make sure it's working fine.

34
00:03:24,050 --> 00:03:24,530
Mm.

35
00:03:25,100 --> 00:03:36,320
Scale percentage, let's say 10 for width now, the width will be image that shape one.

36
00:03:37,010 --> 00:03:37,820
I'll explain you.

37
00:03:37,820 --> 00:03:38,210
Why?

38
00:03:41,430 --> 00:03:42,080
Zero.

39
00:03:42,440 --> 00:03:42,790
Right.

40
00:03:42,800 --> 00:03:43,580
That should do it.

41
00:03:43,880 --> 00:03:45,320
So what is this?

42
00:03:45,530 --> 00:03:50,180
Well, image is the image object to its image that shape.

43
00:03:50,720 --> 00:03:53,210
This one here gave us this double.

44
00:03:53,390 --> 00:04:02,330
So image that shape is a temple and one is the second item, which is the width and zero gives us the

45
00:04:02,330 --> 00:04:04,310
first item, which is the height.

46
00:04:05,000 --> 00:04:05,840
So the width.

47
00:04:06,410 --> 00:04:06,890
The width.

48
00:04:07,820 --> 00:04:08,270
Height.

49
00:04:08,960 --> 00:04:09,410
The height.

50
00:04:09,980 --> 00:04:11,840
I think it makes sense to run.

51
00:04:13,470 --> 00:04:13,690
Yeah.

52
00:04:13,700 --> 00:04:17,390
So this is the new dimensions, width and height.

53
00:04:17,600 --> 00:04:24,020
Now that we have that, we could go ahead and create another function which could see something like

54
00:04:24,380 --> 00:04:28,100
resize for resize image.

55
00:04:28,460 --> 00:04:31,400
And this would get an image path as arguments.

56
00:04:31,850 --> 00:04:39,860
And the scale percentage argument, which is a local argument, a local variable of this function,

57
00:04:39,860 --> 00:04:40,100
right?

58
00:04:40,460 --> 00:04:44,900
And then we would get this here and paste it in here.

59
00:04:45,290 --> 00:04:47,660
So in dense under this function.

60
00:04:47,810 --> 00:04:49,880
And instead of saying Galaxy J.

61
00:04:49,880 --> 00:04:59,450
Peg, we see image path and then C v 2.3 size resize gets as argument.

62
00:05:00,110 --> 00:05:05,810
The source, which is the image object and this size, which I'll tell you what that is.

63
00:05:06,440 --> 00:05:09,450
So image this one in here goes here.

64
00:05:09,470 --> 00:05:19,010
So we want to resize that image object and the disk size now is something that we want to get from this

65
00:05:19,010 --> 00:05:19,490
function.

66
00:05:20,090 --> 00:05:22,670
So that is the size of the new size.

67
00:05:23,120 --> 00:05:33,800
Therefore, I'm going to say here the new design for new dimensions equal to calculate size a scale

68
00:05:33,800 --> 00:05:38,860
percentage is the first argument that that this function gets.

69
00:05:38,900 --> 00:05:45,470
So you see here scale percentage and also width, which is image.

70
00:05:45,830 --> 00:05:47,510
Yeah, this one here, let me cut it.

71
00:05:48,140 --> 00:05:49,190
That is the width.

72
00:05:52,320 --> 00:05:54,300
And the height.

73
00:05:56,010 --> 00:06:03,360
Has an index of zero great, so let me leave that part and that as well.

74
00:06:04,050 --> 00:06:08,460
And then here we see new dim rates.

75
00:06:09,180 --> 00:06:16,170
So the precise method of QE two gets to the image object and the new dimensions as arguments.

76
00:06:16,320 --> 00:06:23,640
But you need to know that this method returns another image object.

77
00:06:24,660 --> 00:06:35,880
So let's call this resized image and then using two dots, I am right to write this resized image into

78
00:06:35,880 --> 00:06:44,310
an actual file, so you could give this a file name for the new image, such as resize something or

79
00:06:44,460 --> 00:06:51,780
you could make things better and see resized path.

80
00:06:52,350 --> 00:07:00,450
So when you call the function, we want to provide that path as a string in the function call.

81
00:07:00,600 --> 00:07:01,920
Not hard code.

82
00:07:02,010 --> 00:07:10,470
The string in here so precise path goes here as the first argument and then the resized image.

83
00:07:11,250 --> 00:07:16,590
That's what I'm right expects the file name and the actual file object.

84
00:07:17,070 --> 00:07:19,230
Now we can call this function.

85
00:07:19,650 --> 00:07:23,100
So what we will do now is we'll call this function.

86
00:07:23,730 --> 00:07:26,430
Given these three arguments.

87
00:07:28,250 --> 00:07:35,570
When we call this function, this function will call the calculates size function, you know, given

88
00:07:35,570 --> 00:07:39,530
the scale percentage which it will get it from here and so on.

89
00:07:39,710 --> 00:07:40,550
So resize.

90
00:07:41,090 --> 00:07:41,810
Image Beth.

91
00:07:42,620 --> 00:07:50,180
This is the original image galaxy the JPEG scale percentage 10 precise path.

92
00:07:51,020 --> 00:07:56,440
How about resized galaxy but pack?

93
00:07:57,470 --> 00:08:03,530
Or you could also creates a variable here, such as name equal to Galaxy Dot JPEG and then use an f

94
00:08:03,530 --> 00:08:04,310
string here.

95
00:08:04,850 --> 00:08:06,020
But let's keep it simple.

96
00:08:06,090 --> 00:08:07,130
We could do that later.

97
00:08:08,450 --> 00:08:12,320
Let's try it then to see what errors we get.

98
00:08:14,730 --> 00:08:24,960
So can't pass this size sequence item within zero has the wrong type this size, so this size was an

99
00:08:24,960 --> 00:08:28,470
argument of right size.

100
00:08:29,310 --> 00:08:36,210
So you see this size is a second argument, which is expected to be a tuple.

101
00:08:36,390 --> 00:08:40,919
So new dim is given here and new dim is actually a typical.

102
00:08:40,919 --> 00:08:47,520
You see, we get new dim from the return of this calculate size function.

103
00:08:47,670 --> 00:08:53,520
So this is a typical you see the parentheses and this is saying that the six item within the zero.

104
00:08:53,610 --> 00:08:58,350
So the first item of this table has a wrong type.

105
00:08:59,610 --> 00:09:04,170
So this item new width, new width was defined in here.

106
00:09:04,380 --> 00:09:06,910
So this is the wrong time, it says.

107
00:09:06,930 --> 00:09:09,300
And I know of that's the type.

108
00:09:09,510 --> 00:09:13,940
Since these are dimensions, it should be an integer.

109
00:09:13,950 --> 00:09:18,630
But since we are dividing this year, this is probably a float.

110
00:09:19,230 --> 00:09:27,720
Therefore, we could convert this into an end and do the same for height, convert them both into ends.

111
00:09:28,140 --> 00:09:33,630
And then we make sure that this will gets an inch and this will gets an int as type.

112
00:09:33,840 --> 00:09:39,240
So run again and you see a resize galaxy was generated in here.

113
00:09:39,660 --> 00:09:41,880
It looks small.

114
00:09:41,910 --> 00:09:46,350
So even though Apple is opening up here, it's stretching it out.

115
00:09:46,380 --> 00:09:55,710
You see that we only have these number of pixels, which, if you like, you could also print it out

116
00:09:56,460 --> 00:10:00,390
in here prints you dim.

117
00:10:03,200 --> 00:10:07,400
And say new, what's new?

118
00:10:08,660 --> 00:10:09,010
Heights.

119
00:10:09,410 --> 00:10:09,790
Run.

120
00:10:09,980 --> 00:10:14,960
And now you get some outputs in the command line as well, which is more user friendly.

121
00:10:15,800 --> 00:10:17,770
So that was the original image.

122
00:10:17,800 --> 00:10:19,190
This is the resize image.

123
00:10:19,490 --> 00:10:23,510
Ten percent was quite a low value to apply.

124
00:10:23,520 --> 00:10:30,380
So usually you want to do something like 50 percent or higher when you want to resize the images.

125
00:10:30,830 --> 00:10:36,170
And that is how you can go resize images, and this is how I'll do it.

126
00:10:36,470 --> 00:10:43,700
So I'll just use these functions to separate things, because the advantage of these now is that if

127
00:10:43,700 --> 00:10:52,100
you want to resize multiple images, then all you have to do is just write a for loop for each iterate

128
00:10:52,110 --> 00:10:54,800
through a list of image file paths.

129
00:10:55,140 --> 00:11:02,600
And then in each iteration you call the resize function and provides an image path of the current file

130
00:11:02,600 --> 00:11:05,870
path of the current iteration and so on.

131
00:11:06,170 --> 00:11:07,430
So thank you for following.

132
00:11:07,580 --> 00:11:08,750
I'll talk to you later again.

