 What is going on everybody? My name is John Hammond. It has been a hot minute since I posted a video So I wanted to bring this to you. This is actually a request an individual Mohammed had asked me hello dear can you help me? I want to write a function through the function get image from a specific path to a particular image in the computer and the Output will be a table as following first column will be the colors in the picture in hexadecimal code And the second column will be how many times it's repeated in the image, okay? So this isn't extremely difficult. We'll just burn through it here And I needed a couple images to be able to do this right so he wants a table which has the We can we can use a function. I'm sorry a module or library called pretty table, which I think is a built-in I didn't have to at least I don't remember ever installing it. There are plenty of others. There's tabulate There's text table. There's other things but pretty table I think is simple and easy enough and We'll use pill obviously to work with the image and I'll use the collections dot counter Submodule I guess in that case to go ahead and actually maybe I don't think submodules right where for that either It's just something in the collections module that allows get the count of all of those pixel colors So let's go and do that. I wanted an image to work with I originally just used my Desktop background but as you can see there is a lot of color in that so be warned that you're gonna have a lot of Output if you use it on a bigger image It's obviously gonna take a lot more time to run as well So I went to Google and I just kind of searched for something like a QR code But that didn't work because it was just the black and white channels black and white I went for and I found this guy and that's in a thousand by a thousand image So not extremely huge not extremely small either so I just wanted to save that image as Like I called it qr.jpeg in that case earlier when I was testing because that's just what I had with the file name I was working with so let's go ahead and do that Sublime text still open cool So I had called this Mohammed dot pn dot pi earlier So I'm gonna have to overwrite that or I removed it sweet I'll use this in Python 3 because the internet will yell at me if I don't and we'll go ahead and import image from pill import We also want counter From collections and we also want that pretty table so import pretty table So the first thing you're gonna want to do is obviously open up that image. I'll use qr.jpeg in my case IMG you can give it whatever variable name you want or whatever object name you want The real thing important thing that we want is get the actual data out of it So image load and if you want to loop through all that data you need to know the size of it So that's actually the IMG object not the data object But you can say size is equal to within heights with they're also equal to image size cool So now we can start to loop through it. I will create a colors list So I keep track of all those things and we'll do for x in range of width and the same thing for y in range of height and range of height Cool and now we can go ahead and get the color out of it So we'll just say colors or data. Sorry in this case x comma y and then let's just to make sure we're actually getting that data We'll go ahead and print that color. So I will use Python 3 on Mohammed pi and that didn't do anything for me So let's take a look at what we got wrong and how that runs very weird 255 255 255 gradually get occasionally getting colors good enough now. Let's go ahead and maybe I just didn't save it earlier. That's funny Let's go ahead and create the hexadecimal representation of these colors So we can do that with some list comprehension. We can just go ahead and loop through C for C in color And we'll take the hexadecimal value of this, right? So let's go ahead and print that out to take a look at make sure we get in the values that we're expecting Zero X FF zero X FF et cetera et cetera Eventually we'll get zeros in there and that'll just be zero X zero So we have two things that we kind of need to account for the zero X in the start can go ahead and be sliced off So two colon just to get that zero X cut away And then we want to write justify it with a zero in case it's a single character single byte So let's go ahead and dot R just and then two and then we'll fill it with zeros to make sure that it's actually the value That it should be now if we look at our black colors all those zeros It should still be zero and everything else for single colors perfect Let's go ahead and join those together with an empty list I'm sorry empty string and then let's just tack on our little Hashtag pound sign there to denote that that is the hexadecimal color. So now we can say colors dot append Hex color Perfect, and now we should have in the colors list a lot of those colors Now let's use counter to go ahead and determine how many of them that we have I'm actually going to create my pretty table here and now because that will allow us to actually Get the display that we want so pretty table dot capital pretty and capital table And then you can define with a list the header values that you kind of want for your row So I'll say color and then count Good, so now we'll go ahead and loop through what we will return from the counter. So let's say color and count in counter of Hex colors or just colors in this case of the variable dot items and Now we can add that as a row to our pretty table So add a row and it takes a list so color and count and that's all we need to do because we're just simply adding that to our Table now we can print out our pretty table object and we should be good to go So let's run this takes a little bit of time to process through that image But as you can see we have a table now with the color and the count all in hexadecimal So again if you were to run this on something that's a significantly larger image with a lot more color It's going to take a lot of time. I don't know entirely the use for this But I wanted to explain. Okay, this is how you can do it. Maybe this is good for quick and easy analysis or Something but as you can see it's taking a long time to process through the image and once we get output return to us It will be a lot I'm even building the hype up now. Okay there now. We've got Many many rows to this table my terminals buffering for a good amount of time So maybe that will be useful to you somehow, but that is how you do that Pretty table. I think it's pretty neat. It was a good excuse for me to take a look at it But it's super simple all you need to do is just to find the header values and then add rows as needed Just print it out because this is the object will go ahead and draw itself on the screen for you So that is that hope you enjoyed this if you did like this video Please do like comment and subscribe join our discord server. There's a link in the description It's a cool community full of CTO players programs and hackers a lot of smart people They're certainly much smarter than me and if you would like to support, please check me out on patreon Thanks so much. See you in the next video