Professional project
Python code solution and explanation
colorthief, matplotlib, RGB, HEX
introduction
An image palette generator is a tool that can analyze an image and extract its dominant colors to create a color palette. This palette can then be used for website design, graphic design, and other visual arts, to create a consistent and appealing color scheme based on a specific image.
The program we’re going to make today will extract the most dominant colors in an image, show what these colors look like, and print out the RGB and HEX values. So why use a paid service or website to access this, when you can just code it yourself, in Python? Let’s get started!
SOLUTION
First, let’s import the libraries required for this project. colorthief
is a simple python module to grab the color palette of an image. matplotlib
is a plotting library for visualizations.
from colorthief import ColorThief
import matplotlib.pyplot as plt
First, I used an image I created in paint, called default.jpg.
To get the most dominant color, we can use ct.get_color(quality)
. This will get the most dominant color in an image. quality
will be 1, and any higher will possibly result in a worse image.
ct = ColorThief('default.jpg')
# dominant color
dominant_color = ct.get_color(quality=1)
Output: (139, 4, 20)
Now let’s show the image using plt.imshow([[dominant_color]])
. Inside needed will be an array-like value.
plt.imshow([[dominant_color]])
plt.show()
We got the most dominant color, but how can we generate a palette? I’m going to use another image, forest.jpg.
We can use the ct.get_palette(color_count)
method and set that to palette
. Inside, we can set color_count
to how many colors we want generated. Then, we can use list comprehension to display each color in palette
.
palette = ct.get_palette(color_count=5)
plt.imshow([[palette[i] for i in range(5)]])
plt.show()
Finally, we can print the RGB and HEX values of each color in palette
by running a loop. To convert to HEX, we have to convert each value in the RGB by using color[n]:02x
.
for color in palette:
print(f"RGB: {color} | HEX: #{color[0]:02x}{color[1]:02x}{color[2]:02x}")
Output: RGB: (43, 45, 55) | HEX: #2b2d37 RGB: (206, 110, 90) | HEX: #ce6e5a RGB: (108, 140, 152) | HEX: #6c8c98 RGB: (140, 164, 180) | HEX: #8ca4b4
final code
To view my full code, please visit my GitHub repository:
https://github.com/Gursehaj-Singh/image-palette-generator
from colorthief import ColorThief
import matplotlib.pyplot as plt
ct = ColorThief('forest.jpg')
# dominant color
dominant_color = ct.get_color(quality=1)
plt.imshow([[dominant_color]])
plt.show()
# color palette
palette = ct.get_palette(color_count=4)
plt.imshow([[palette[i] for i in range(4)]])
plt.show()
# print palette
for color in palette:
print(f"RGB: {color} | HEX: #{color[0]:02x}{color[1]:02x}{color[2]:02x}")
Further Steps
Now that we’ve created a simple image color palette generator, we can make something more user-friendly interface. For example, adding GUI, allowing the user to navigate and upload their image, and defining how many colors they need.
Hope you enjoyed my program.