Extracting Dominant Colors From Images in Python
Key Takeaways
Extracting dominant colors from images using Python, utilizing libraries such as OpenCV and scikit-learn for image processing and color analysis.
Full Transcript
what is going on guys welcome back in this video we're going to learn how to extract dominant colors from images using python so let's get right into it [Music] all right so we're going to learn how to extract the most dominant colors from images using python and this is something that i'm actually currently doing in a real project that i'm working on i'm taking images and extracting the colors to create new features because sometimes you have images like for example this one here of a shirt with some colors here and you want to get certain information about that shirt for example you want to have the sleeve length or you want to have the shape the size whatever or you want to have the two most dominant colors for example so gray dark gray and something that's kind of blue or anything like that something like that um but you don't want to take the whole picture the whole pixels and feed them into a convolutional neural network you want to have just certain features and what you can do in such a case is you can extract the two most dominant colors then you're going to get an rgb code for example and you can define certain ranges you can say okay this is blue this is light blue this is green this is lime whatever and then you classify the colors and you have certain numerical labels for the individual categories and this is data that can then be uh fed easily into a machine learning model and this is what i'm doing in the project and this is why i'm making this video because i think it's kind of interesting to see how you can extract new features from images without having to feed the full image into a neural network for example so this is what we're going to do today and for that we're going to need a library called color thief and this is installed using the command line so pip install color thief like that and if you don't have it installed already you're also going to need matte plot lip not necessarily for the extraction but for the plotting of the actual colors if you want to see if you want to display the colors the results and we're going to say here from color thief import color thief and from or actually not from import matplotlib dot pi plot splt so what we can do now is we can create a color thief object we can say ct equals color thief of test image dot jpg and we can say print ct get color quality equals one and this will give us the one most dominant color in this case this is the rgb code 37 for red 41 for green and 42 for blue now we can also store that we can say this is the dominant color and this is equal to that and then we can say plt in show important we need to pass a list of lists um and here we need to pass the tuple dominant color plt show otherwise we're going to get some bizarre uh visualization but this here is now just the color uh you can see it's a dark gray and this is basically how i get the one most dominant color now i'm not sure if for that here this should be red i think a dark red so let's see what happens if i say test image five you can use by the way whatever images you want as you can see here this is a dark red uh the background is usually not considered so i i don't know if white is considered at all but um you can see that it it extracts the most dominant color now this is if you just want to have the one most dominant color often times like in that image for example it makes sense to have two or three colors because if i tell you as a customer the main color of this shirt is black or dark gray you're going to be misled because as a consumer you would probably say it's also blue because blue is a very obvious color in this shirt so we want to have two or three dominant colors and not just one how can we do that quite easily we can say here that the uh palette is equal to ct get pellet i hope this is how it's pronounced pellet poletti no probably not poletti pellet i think um and then color count equals and we can say for example give me the five most dominant colors and then what i can do here is i can say plt in show and here again a list of lists and here we say palette or palette and then i for i in range five and then of course we need to also do a plt show otherwise we're not gonna see anything and then we're gonna get the five most dominant most common colors in this case this is now the red shirt so we have a lot of red and if i change this to the first one we're gonna see that it's gonna have the dark gray from before and it's also gonna have all the blue tones and a lighter gray as well so that's the most basic way this is actually it so if that's what you wanted of course now it's a little bit more complicated to categorize because you get rgb values and if one number is different then it's a different color so you need to think about how to categorize those but this is already how you extract a dominant color so you can stop watching here if you want to but i want to show you one more thing here i want to show you just in case you need it how you can take those rgb values and convert them to hex values to uh h what was it hsv and hls i think it was hue saturation value and hue lightness saturation or something like that um and in order to do that you need to import a library called colorsys now we can import it down here this is part of the core python stack by the way um or you can import it up here in top and we can now say for example for color in palette uh we can say here that we want to print the value first of all the color itself this is the rgb value then we want to print it in hex and for hex we're going to use string formatting we're going to say that this is hashtag because it's hex and then color 0 which is the r value and we're going to format this as 0 to x which will make it a hexadecimal number then we're going to do the same thing with color 1 0 2 x and color 2 0 2 x like that and um in order to convert to the different um display methods so h sv and hls what we do is we say colorsys dot rgb to hsv and then basically just unpacking color because that unpacks the tuple into the parameters that we need here we need rgb and the same can be done with hls and if i run this now after we see the colors here you can see that this is the gray color here we have the hex code and here we have the h s v and the h l s codes so no matter what you're using for your colors um in in your system in your database whatever you have in your data frame you can use that notation as well here and you can then categorize by distance by whatever i'm gonna make another tutorial soon on how to name colors so how to take the color find the closest color and assign a name to it this can be quite useful as well but that's it for today for how to extract the most dominant colors from images using python so that's it for today's video i hope you enjoyed it and hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] you
Original Description
Today we learn how to extract the dominant colors from an image in Python.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: CV Basics
View skill →Related Reads
🎓
Tutor Explanation
DeepCamp AI