Python image manipulation: removing a color from a picture

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay we're gonna take a look at image processing with python and images are made up of pixels each pixel has an x and a y coordinate uh the x goes across exactly like it would normally uh but the y may be a little bit different from what you're used to we actually we actually use the lower right hand quadrant we don't start down here in the lower left we start up here in the upper left and um and i assume that's because you know normally uh in the united states we start at the top of a document work our way uh crossing down although if we want to work our way across first we would increment the y pixel more slowly and we in the inner loop we would increment the x pixel move all the way across the examples i've got are the other way around uh so we increment the x pixel on the outside loop and then the inside loop we do the y pixel and so we're going to do a column and then a column and a column of column column it doesn't really matter because in the end it's all going to be the same so in order to do this you need to have this downloaded and available to python and if it's not included with your python distribution you can google it and find out how to download it uh it's uh relatively simple to download um so if you want to create an image this is one way to do it if you want but we're not going to do anything with this uh this just says the dimensions of the image are going to be 640 by 480. that's wide and tall and uh the color of every pixel is going to be zero for the red zero for the green and the maximum 255 for the blue so we're just going to get a blue rectangle which isn't all that interesting uh what is more interesting is uh opening up a file that has a picture in it and this is all you have to do is give the file name right here this happens to be in a folder called pix so uh we've got a little path information here so this assumes that in the in the folder that the program is being run out of there's a subfolder called pics and inside that subfolder there's a file called flowers2.jpg so when you want to access every pixel it's a very simple algorithm it's that same nested loop algorithm that you've seen a lot of times before we the outside one is going to take x from now image is the name of our picture here okay and it has a property called size that has two elements in it the first one is the width and that's size sub zero and the second one is the height and that is psi sub one so the outer loop x will change slowly and then we'll go through all the y values in the middle and then we'll go back out and change the x then we'll go through all the y values in the middle and so on and at each one of those coordinates for x and y we want to get a pixel and we have to pass in x y in parentheses uh so the argument here is uh it's going to highlight the whole thing for me the argument is from here uh to here uh it is that's called a tuple in python and so we're passing in a single tuple uh which can which is similar to a list but it can't be changed okay and it's going to return three things we haven't seen this i don't think before either it's going to return three things the red component that i'm going to store in r the green component that i'm going to store in g and the blue component i'm going to star in b so red green blue is frequently abbreviated rgb so anybody who's reading this that knows anything at all about images would understand that those are reasonable variable names even though they're only one letter long if i want to change a pixel uh it's the name of the image dot put pixel and it has two arguments this might look like it's five arguments but the first argument starts here and ends here and the second argument starts here and ends here so this is a tuple with three elements or two elements uh this is one with three elements but there's only two arguments are being passed as you can see because there's one comma here between the first argument and the second argument so what we're going to do is we are going to take a look at sample program and file name and to save myself a little bit of typing here i'm just going to do or and i've got one out there called roses dot jpeg okay and i will open it and i will refer to it from that point on as pick and the first thing i'm going to do is i'm going to show it that will open up an image manipulation program on my computer and it will just display the original picture and then here is that standard loop that i talked about it's going to be exactly the same every time so we visit every x pixel and when we're on a given column for x we visit every y value in that column then we go to the next column and we're all the way down and so on and uh what i'm going to do here is i've modified this program a little bit i'm gonna go back to okay so if i do this it's just gonna put the exact same red green and blue components out there i'm gonna get rid of the red and put a zero in there so zero means no red 255 would be maximum red and um so every single pixel is going to have the red taken out and i'm going to end up with a picture that's made up of only green and blue components so let's go ahead and run that and when i get over here and hit enter it'll take the roses file and there is the picture with the red removed so all you can see here is you know it's mostly uh the other color in the leaves apparently was was more green than blue and in the background here it looks like it was a little more blue than green i'm not sure what happened to my first picture here that should have been first picture displayed and we missed it somehow uh it didn't come up on top so that's what the original picture looked like and this is what the picture looks like when you take all of the red out of it okay so we'll try a few other ones here uh we will leave the red in and we'll take all the green so this isn't going to change it a whole lot because red is the dominant color in that picture but let's go ahead and run it and i'll hit the enter key okay now it's showing up that's the original and in a few seconds we'll see what it looks like with the green removed okay so uh if i take the green out of the background what's left is the blue and the red if i take the green out of the red here um there's not much blue in there uh so it's still going to look very red it's going to look even more red though than it did before and uh there's no green in the outside here so i'm going to get what's left is going to it's going to be red and blue which combined gives us a shade of purple and the last one we're going to do is we'll leave the green the same and we will put a 0 in for blue this one probably all also will not make a whole lot of difference because red is the dominant color okay so if i take the blue out what i'm left with here is going to be the red and the green and if you mix red and green together in equal amounts you get yellow and so in the lighter parts here it's pretty much equal amounts uh in the darker parts here there must be a little more blue i'm sorry a little more green in there than blue but the original picture is still very recognizable okay uh now my comment doesn't really apply anymore so i think i should probably get rid of that uh now let's go and put the blue back in and i'm gonna try something this i'm gonna try dividing everything by two so i've got half as much so if i don't have as much uh the picture is going to be it's not going to be lighter or fainter it's going to be darker because i'm putting less color in i'm putting less light in so let's go ahead and run that and we'll hit enter for our file name and there's our original the next version that pops up should be a darker version of that and it is okay so it looks like a picture that's underexposed let's go ahead and let's try dividing by four this time and that should darken quite a bit darken it quite a bit more so there's our original and there's a much darker much more underexposed version of the original pictures that's what happens if you cut everything in four by or divide everything by four okay so that's that's one example we'll do a few more in some upcoming videos you
Info
Channel: Tom Kleen
Views: 7,240
Rating: undefined out of 5
Keywords:
Id: YwniLK-GWIY
Channel Id: undefined
Length: 9min 38sec (578 seconds)
Published: Wed Oct 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.