Extracteur de Palette de Couleurs d'Image
Extrayez les couleurs dominantes de n'importe quelle image et obtenez leurs valeurs HEX, RGB et HSL.
Cliquez ou déposez une image ici
Questions Fréquentes
Implémentation du Code
# Extract dominant colors using colorthief
# pip install colorthief Pillow
from colorthief import ColorThief
from PIL import Image
def get_palette(image_path: str, num_colors: int = 6, quality: int = 1):
"""
Extract dominant colors from an image.
quality: 1=best (slow), 10=fast (less accurate)
Returns list of (r, g, b) tuples.
"""
ct = ColorThief(image_path)
# Most dominant single color
dominant = ct.get_color(quality=quality)
print(f"Dominant color: RGB{dominant} -> #{dominant[0]:02X}{dominant[1]:02X}{dominant[2]:02X}")
# Full palette
palette = ct.get_palette(color_count=num_colors, quality=quality)
print("\nPalette:")
for i, (r, g, b) in enumerate(palette):
hex_color = f"#{r:02X}{g:02X}{b:02X}"
print(f" {i+1}. RGB({r}, {g}, {b}) -> {hex_color}")
return palette
# Alternative: k-means clustering with scikit-learn
# pip install scikit-learn numpy Pillow
import numpy as np
from sklearn.cluster import KMeans
def kmeans_palette(image_path: str, n_colors: int = 6) -> list:
img = Image.open(image_path).convert("RGB")
img = img.resize((150, 150)) # Downsample for speed
pixels = np.array(img).reshape(-1, 3)
kmeans = KMeans(n_clusters=n_colors, random_state=42, n_init="auto")
kmeans.fit(pixels)
centers = kmeans.cluster_centers_.astype(int)
counts = np.bincount(kmeans.labels_)
sorted_colors = centers[np.argsort(-counts)] # Sort by frequency
result = []
for r, g, b in sorted_colors:
hex_color = f"#{r:02X}{g:02X}{b:02X}"
result.append({"rgb": (r, g, b), "hex": hex_color})
print(f"RGB({r}, {g}, {b}) -> {hex_color}")
return result
palette = get_palette("photo.jpg", num_colors=6)
print("\n--- k-means ---")
kmeans_palette("photo.jpg", n_colors=6)Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.