 One of my favorite ways to demonstrate sort of the use of nested lists is through the application of Image processing now the way to think about image processing, especially when we're dealing with Sort of colors is mixing of colors Now this is where we first have to do a little bit of terminology because we're very used to paint, right? you know I happen to have just a Clear water bottle and I happen to have some three colors or GB red green blue now The issue is that when we're mixing something like dyes Let's say for example. I mix red in to my clear palette for a second If we're just working off of the mixing of dyes What we're doing is we're actually doing something known as a subtractive color model, right? I've removed some of the light That is potentially going to be How's the best way to describe this? The only thing currently being absorbed is the red light spectrum and so as a result we see red light But the issue comes in that as I begin to add in more colors And let's actually just say I add in blue for a second As I add in blue and assume I am because I'm not holding the bottle up as I add in that blue dye Yes, I'm getting it's kind of purple But I'm getting a very purple color because now blue light and red light are being absorbed but the issue is that this is getting darker and Just to finish it all when I add finally that that green mixture into this one too The same kind of effect is going on and as I add color to sort of my my Palette this is getting darker and darker until it's just absorbing all of the light spectrums The issue is that light is not like this when we add color to light We actually are doing an additive color model And so let's say for example if I started with red light. Well, if I'm just Shooting out red light. I'm just going to see red light But if I were to add green light to that what we're actually going to get is that mix And in fact, we're going to now see yellow light as a result instead of the mixing of red and green to produce brown And that's the same kind of thing if I add red light and blue light I'm not getting purple, but I'm getting sort of a lighter Pinkish known as magenta and the same kind of thing comes in when I were to add all three of those colors rather than getting a black Because I'm now absorbing everything. I'm reflecting all different light spectrums and that's actually what produces our white light Okay, so why do we need that to know sort of just that part first? Well, when we're dealing with image processing, we're not dealing with the mixing of dyes We're not mixing colors and slowly some things get darker. We're actually making them brighter So this is actually where we get into the idea of sort of pixels and the color spectrum on pixels Now they typically operate off of eight bits And if we were to go all the way back to binary representation, we can represent 256 numbers using eight bits or 0 to 255 And so we're starting to think about different colors. So say for example Mario over here Let me use a different color than blue to illustrate this We'll go red if I'm looking at the sky For example Well, it is some combination of red green and blue light that is producing sort of this Color of blue and that is represented red green blue or RGB so With this sort of representation the red even though, you know, it is not a red Blue or a red color. It does still have a little red in it. And so we can say that. Oh, well, it's gonna give me We're gonna say it's gonna have a value of 80 And then the green is gonna have you know a halfway point It's gonna have a color of 128 and then we're going full max on the blue 255 and it's through this combination of RGB that we do in fact now get sort of that blue and the same kind of thing goes on There's two separate colors of green going on here a darker and a lighter green They are also being represented by RGB's and you can see that the green value is The largest of the three going on there Another point though is that you could take a look and see that. Oh, well that happens to be just a number of rows and Going left to right. We're dealing with just a whole number of columns and Every single row in every single column is just a teeny tiny little square even smaller than what I'm working off of That we would represent as a pixel And each one of those teeny tiny little squares needs to have an R a G and a B and it is through the Combination of that red that green that blue that as you can see with this gif as they go They increase and decrease we start to see different colors. So as blue begins to increase if red is also Fully maxed out then we get a magenta, but if we add all of them maxed out We get white light as we remove the red. I'm just kind of following along. We get sort of this cyan color Now the point I was showing off just a moment ago is when we're dealing with these rows and columns This is a little different than how we learned geometry, right? In your geometry class, you typically talked about some origin point where zero was like always at the center And oh you could have negative values Going on there because you needed to know negative values Well, there is no negative technically in an image, right? What's the negative one pixel of my my graphic? It doesn't exist So this is some of those points that we have to think about another one is that when we're dealing with our Y axis, we've always assumed or always playoff this and this is something that even I you know working in image processing I forget sometimes why we always assume and think of as going up. So, you know, this is the one This is the two. This is the three But if we're dealing with images and we're treating them very similar to how we were working off of a nested list We're talking about this as being our first or our zero throw and then our first row two row three row four row Five row etc all the way down So as a result our positive Y values are actually going Down instead so the the larger Number and why instead of it being really high up there. It's actually really low down there X stays the same It is still just left to right in our world. So this would still be our one still be our two three four and five So you can sort of see that this X if we're playing off of my sort of Analogy, yes, it's a rectangle, but work with me would be effectively at the two row at the two sort of column if you will So with that in mind again We can take any one of those points and we can start to make different color combinations And if we work off of just a hard, this is the only color You know, I'm only doing red and yes We get these very bold bright colors that we're all very Commonly seeing you know going on there and then we get some Combination of them both so red and green would produce yellow But it is through again some combination of the red green and blue that we get all of these nicer You know softer colors that you would you know study color theory to know why like this yellow is better than that yellow I don't I don't know Okay, so we now have done a deep dive on color theory. You're all experts in painting or painting with light So how do we start to take this application and apply it to? Python and nested lists So the way to think about this is again, we're dealing with a multi-dimensional list So in our case, this is still a row with some Indices attached to it or some row with indices attached to it In some column so in theory these are just kind of going on there as Well, and they're all separated by commas and one of the wonderful things about this is we could do math Right, they're just numbers and math is just playing with numbers So something like grayscaling an image well if I can traverse all of these indices Well, this just happens to be three numbers and if I were to add those three numbers together and divide them by three so See that's a ten two Carry the one let me just cheat and do this skip ahead if you don't want to do math, but Come on You're watching a programming video. So of course you want to do math So we're dealing with a 12 carry the two 12 13 So one two four five So five thirty two divided by three. That's one three two Drop that down. So that's a seven seven twenty one Two two roughly speaking a seven seven seven. I'm not going to Use Decimal places here, but roughly speaking I could say oh well, let me just put a seven seven seven in the RGB values And once again the way to think about what will happen is if I increase all of these colors to We'll have to wait a second if I increase all of these colors to be the same value I'm going to have some variation of either White or black and so two fifty five two fifty five two fifty five we get a white Color that's changing really quickly We're doing that again with grayscale if we did that with all of our pixels Not just that one that I was working off of but all of them you could see that again There's that one seven seven and I threw it together in all the in the red green and blue And so we get some kind of gray and if I did that with every single pixel I would just get once again a grayscaled variation And so you can see that in effect. So this happens to be Montreal Convention Center I was taking it while I was there if I were to average each pixel and then replace that average with that color That's exactly what I would get Okay. All right now. Let's do it. Let me So There's a lot going on here, and I will actually walk through it as we go through it so the big ways to think about this is The first thing we are operating with is something known as the pill library pill is a Python image library, but the entire purpose is to take pictures png files jpeg files and Convert them into the nested lists that we need to work off of now It isn't just as fancy as a nested list. It goes into a little bit more of the object world But we can start to work off of it The next line is dealing with just what my file name is going to be I happen to have That's not what I wanted I happen to have an image in My little folder called peppers and if I were to look at that it's a Plethora of peppers reds and green peppers and you know banana peppers, but that's all it is just a bunch of peppers So I'm just going to go ahead and call that as a variable That way I can work off of this and we'll deal with file IO a little later on But the idea here is now I need to store that file that digital ones and zeros in my computer Or in my program and I can do that using the pill library. So in this case image open will Say, oh well, give me the file path and here's the file path and okay. I have my image now This has a number of different Settings and functions and behaviors and states Associated with it for example the width and the height of the image stored as a list So I can extract out say for example the width by doing IMG dot size and then Square brackets zero that'll give me the width The width of the image and then the height of the image same thing image dot or IMG dot size square bracket One that'll give me the height of the image now once again. This is stored very much in a nested list Situation it's not 100% the same as when we're just dealing with nested lists But it still has rows and columns and so for this first line of code We're dealing with traversing each row and you can see so I've got a variable I'm gonna call row and I am creating a range from the zero to wherever the height is remember y starts at the top or Why zero starts at the top and goes down as we traverse through the image and then same kind of thing I have something for the columns going to on with my image. So for each row for each row go through each pixel Over and over and over again Now we do need to extract out colors and we can do that by doing a few things now to start This is known as unpacking a variable or a list and the entire idea to this is if I were to just let me comment all of this out and just print Get the color at that particular row and column or that pixel I'm just going to see a List of numbers, right? My reds in my greens in my blues at any particular pixel The problem is that well, you know, I know that they're always going to be RGB red green blue And so what I can do in Python is I can do something known as unpacking them red green blue Now what this is going to do is again if I look at that first pixel 101 0 0 what I'm effectively saying is take that 101 and put it in the red variable take that 0 put in the green variable take that other 0 put in the blue variable and same kind of thing if I were to print red green and blue I Should see the 101 0 0 What did I do wrong? Oh? One too many races. I still see the 101 0 0 Well, guess what now that we have those numbers at those pixels We can play with math. We can do any calculation that we happen to want so What I happen to have here is just taking for example each one of those pixels and Saying only give me 75% of that value nothing terribly crazy. I'm you know making some weird You know darkening Image filter, but not doing anything outlandish and so now I take those and I create a new list Or tuple as it is called With that new color with that new red that new green that new blue now this last little 255 is just representing Transparency how transparent do I want this particular pixel? 255 not transparent at all. It is very opaque So not see-through is what I'm trying to get out there And then I'm just coming in and saying oh well Let's take whatever new color I've created and at that same row and column make that the new color for that particular pixel and so when I magically take my Peppers image with the green and red well the red and green peppers and I run this I Should get the nice little notification and I'll get this. This is Hideous, I don't like this color at all, but you can see it's like got a hint of red going on there But not quite grayscale. Well, let's actually take that idea of grayscale, right? We we showed that we can do it. So let's produce a gray or gray Either way works and then say red plus green Plus blue Divide it by three take that gray Save it run it. Oh got a float. Ah did this earlier. So it is only expecting an integer number So whatever we get again just like when I did it by hand on the PowerPoints, you know, we are going to ignore Decimal places so we're gonna just drop that by turning it into an integer and What we should see is a grayscale version of our peppers And that's exactly what we get and we can continue on with this through a number of different ways So for example just to skip ahead you can do other types of mathematical notations So for example blurring an image very similar You just take sort of the the pixel that you're currently at say for example, you wanted to be right here I want to blur a an image Then I would just look at my neighboring pixels and do the exact same average instead of doing RGB and dividing by three take all my two my reds take all these reds for example add them together and divide them by zero or in this case nine You do that for again, then the green and then the blue and you would get a blur effect And that's where you could play with some again This is more showing you applications, but the idea would be well now if I worked off of something like a radius Or how big do I make that box? How many pixels do I average around me? I could work off of different blur filters. So our one pixel blur filter Doesn't do much of a change especially for a high resolution image But if we were to increase that to something like 10 pixels now you can see though Well, we're starting to get a blur and if we continue on and we did something like a hundred pixels In my case, we're dealing off of something that is alright You know not 100% but we're still seeing sort of the colors going on there You can even go even crazier. I know that Instagram and you know snapchats and Tiktoks have gone crazy and sepia is no longer the the coolest filter in the world I think it's making yourself look like a Pixar person, whatever But if I wanted to do something like sepia, I just take my old red and if I follow this formula of taking the old red times it by 0.393 and old green in fact, you know Let's just throw this on there and see what we've got. I've got it on the opposite screen here So you don't have to worry So new red is again gonna be red times 0.33 plus green times 0.769 Plus blue times 0.189 Let's just make the new red new green new blue and then let's see old red. That's a little lower a little lower 168 and then the new 22 534 1 3 boom Okay, so we've just gone in we've done this crazy new formula of sepia, so new red new green and New blue so once again we take it we can pile and run our program Did it yeah same kind of thing forgot to convert it into integers afterwards So we were to take that boom boom Save that run that Congratulations. Here's my sepia filter for the peppers image Go forth and create the new Instagram and when Facebook buys you out. I'm expecting like I don't know 1%