1
00:00:00,150 --> 00:00:07,180
In this video, I'll create a script which converts color images, multiple color images into grayscale.

2
00:00:07,200 --> 00:00:12,330
So this is a solution to the exercise that you just got in the previous article.

3
00:00:12,720 --> 00:00:14,190
So this is my rifle.

4
00:00:14,190 --> 00:00:15,600
I have this empty main.

5
00:00:15,600 --> 00:00:16,740
That's why file here.

6
00:00:17,010 --> 00:00:27,720
And then I have prepared two folders, great images here and images images contains these three files.

7
00:00:28,170 --> 00:00:31,470
So three JPEG color images.

8
00:00:32,189 --> 00:00:40,730
So the purpose now is to convert these into grayscale and place them inside great images with Python,

9
00:00:40,740 --> 00:00:41,160
of course.

10
00:00:41,520 --> 00:00:42,390
Let's do that.

11
00:00:42,930 --> 00:00:44,250
What can we do first?

12
00:00:44,730 --> 00:00:47,130
How do you approach this problem?

13
00:00:47,490 --> 00:00:51,720
So when we perform multiple operations, we think about four loops.

14
00:00:51,900 --> 00:00:56,430
Therefore, it makes sense to create some sort of four.

15
00:00:57,180 --> 00:00:59,500
But what do we iterate through?

16
00:00:59,850 --> 00:01:06,000
We should iterate these file paths right away and do something with each of those file paths.

17
00:01:06,570 --> 00:01:09,420
Therefore, we need a list of those for pass.

18
00:01:09,870 --> 00:01:18,570
So I'm going to create a list using the OSU library, even though you could also use the Globe Library.

19
00:01:18,750 --> 00:01:19,860
I prefer OS

20
00:01:22,470 --> 00:01:32,250
and the list of methods of OS, which expects a path to a folder that contains the files for which you

21
00:01:32,250 --> 00:01:34,520
want to create a list for.

22
00:01:34,530 --> 00:01:38,550
So images that one is the path.

23
00:01:39,360 --> 00:01:45,990
And if a print images now and run, we get a list.

24
00:01:46,380 --> 00:01:55,320
It's a Python list and its image has its file name actually is the file path relative to the images

25
00:01:55,320 --> 00:01:55,950
folder.

26
00:01:56,220 --> 00:02:02,430
So the file path for Bear is there, the JPEG galaxy, the JPEG and so on.

27
00:02:02,850 --> 00:02:07,710
Now we can iterate for image in images.

28
00:02:09,030 --> 00:02:11,280
What should we do to each image?

29
00:02:11,490 --> 00:02:16,160
Well, first we want to open needs an open CV.

30
00:02:16,800 --> 00:02:19,590
For that, we need to import CV to.

31
00:02:20,590 --> 00:02:26,380
And then we say here, Kavita, to Dutt, I am reads.

32
00:02:27,520 --> 00:02:31,000
So we should provide the file name, right?

33
00:02:31,750 --> 00:02:36,350
Therefore, we see image, but this is not enough.

34
00:02:36,370 --> 00:02:37,270
You'll see why.

35
00:02:37,810 --> 00:02:40,420
Anyway, let's leave it for now and flag.

36
00:02:40,690 --> 00:02:43,540
We said, that's who we warned Grayscale Images.

37
00:02:43,540 --> 00:02:47,980
So let's give this as a grayscale and store it in a gray variable.

38
00:02:48,220 --> 00:02:52,300
So this variable contains the image object in grayscale mode.

39
00:02:53,380 --> 00:03:04,330
So once you create this great image, you want to run, I am right, which gets a file name.

40
00:03:04,660 --> 00:03:07,390
So this is the file name for the new image.

41
00:03:07,990 --> 00:03:17,500
And if you just give it a file name such as F, I saw an f string and you could do something like gray

42
00:03:17,680 --> 00:03:18,220
dash.

43
00:03:19,000 --> 00:03:27,610
And then you put the variable image, which in the current loop contains the bear, the JPEG image in

44
00:03:27,610 --> 00:03:30,400
the next, it will contain galaxy that JPEG and so on.

45
00:03:30,940 --> 00:03:36,460
So the file name will be Gray Dash, etc. That's a JPEG.

46
00:03:36,730 --> 00:03:37,060
Right?

47
00:03:37,390 --> 00:03:40,420
Let's run and see what errors we get.

48
00:03:41,050 --> 00:03:44,110
So first, we need to provide that image, of course.

49
00:03:44,650 --> 00:03:53,560
But even though we provide the object, the image object will gets another error, which is a bit difficult

50
00:03:53,560 --> 00:03:54,580
to understand, actually.

51
00:03:54,760 --> 00:03:58,000
So it sees that image empty in function.

52
00:03:58,000 --> 00:04:03,550
I'm right, which means that this thing here gray is empty.

53
00:04:03,790 --> 00:04:07,540
We could verify that by printing gray out.

54
00:04:08,230 --> 00:04:11,200
So print gray and you'll see it's none.

55
00:04:11,590 --> 00:04:15,130
That is because we have an issue here with paths.

56
00:04:15,400 --> 00:04:24,160
So when we do for image in images, we do get the image correctly as you see so bear.

57
00:04:25,380 --> 00:04:29,590
Was printed out here, and then we got none for Gray.

58
00:04:29,610 --> 00:04:31,110
The gray version of beer.

59
00:04:31,410 --> 00:04:37,440
So something here is not working well and that something is because, you know, if this mean that's

60
00:04:37,620 --> 00:04:41,220
why file is in this project directory here.

61
00:04:41,880 --> 00:04:50,010
And that means that if you give the script a file name such as Bear the J Pack as in this case.

62
00:04:52,200 --> 00:04:57,870
This script will look for Baird, the Jeep back in the main directory.

63
00:04:57,960 --> 00:05:06,120
So in the same directory where the script is, so it will look here, but there is no bare JPEG file

64
00:05:06,120 --> 00:05:07,230
in this directory.

65
00:05:07,710 --> 00:05:11,490
There is a bear, the Jeep, but that is inside.

66
00:05:16,340 --> 00:05:20,150
Images slash Baird, the Jeep back.

67
00:05:20,660 --> 00:05:23,480
So now it should work.

68
00:05:23,870 --> 00:05:32,120
So we get grey printed out the matrix and then we get printed out galaxy the jet pack for the next iteration.

69
00:05:32,390 --> 00:05:41,030
For that, the Galaxy Matrix and then Python, the jetpack and the Python matrix for that image.

70
00:05:41,420 --> 00:05:44,860
You see this to 55 to 55 to 55.

71
00:05:45,370 --> 00:05:48,540
And these are the pixels for the whites.

72
00:05:48,690 --> 00:05:57,920
Once the white pixels, which means you have 255 amount of reds and 255 amount of green and 255 amounts

73
00:05:57,920 --> 00:06:02,810
of blue, which gives a white pixel, that's sort of combination.

74
00:06:03,680 --> 00:06:06,170
Now we still have another issue here.

75
00:06:07,130 --> 00:06:15,830
You see that the grayscale images were not generated inside gallery images, but inside this main directory

76
00:06:16,160 --> 00:06:23,090
because of course, here when we grow them again, we just declare the file name.

77
00:06:23,330 --> 00:06:29,870
So since the script is in these main directory, it will just write a file in that main directory.

78
00:06:30,170 --> 00:06:35,930
Therefore, here we could also do grey images slash that.

79
00:06:36,230 --> 00:06:42,470
So now we give a string, which is a complete file path, including the folder.

80
00:06:42,650 --> 00:06:49,670
So if you see in gray images now, after I ran the script, you see the grayscale versions of those

81
00:06:49,670 --> 00:06:50,090
images.

82
00:06:51,130 --> 00:06:55,240
So I don't want these JPEG images here, so I'm just going to delete them.

83
00:06:55,450 --> 00:07:03,130
You can delete them here one by one, or you can go to the shell and to our EM four, remove all and

84
00:07:03,790 --> 00:07:04,750
everything for it.

85
00:07:04,870 --> 00:07:11,500
So asterisk means everything that JPEG wrong, that those images will disappear.

86
00:07:11,740 --> 00:07:19,030
But no, the was inside the folders because here we are in the main directory in that folder.

87
00:07:19,300 --> 00:07:23,470
So all that JPEG images of that folder were were deleted.

88
00:07:23,840 --> 00:07:27,460
And that is how to convert multiple images to Grayscale.

89
00:07:27,610 --> 00:07:28,220
Thank you for.

90
00:07:28,690 --> 00:07:29,560
I'll talk to you later.

