import gradio as gr import numpy as np from PIL import Image from sklearn.cluster import KMeans def extract_colors(img, num_colors): if img is None: return "
No image uploaded.
" # Resize image for faster processing img_resized = img.resize((150, 150)) data = np.array(img_resized) data = data.reshape(-1, 3) # Remove any grayscale or alpha if present if data.shape[1] == 4: data = data[:, :3] elif data.shape[1] == 1: # If grayscale, replicate to RGB data = np.repeat(data, 3, axis=1) # Fit KMeans kmeans = KMeans(n_clusters=num_colors, random_state=42) kmeans.fit(data) # Get cluster centers colors = kmeans.cluster_centers_.round().astype(int) # Calculate percentages labels = kmeans.labels_ percentages = [(np.sum(labels == i) / len(labels)) * 100 for i in range(num_colors)] # Convert to hex hex_colors = ['#' + ''.join(f'{c:02x}' for c in color) for color in colors] # Generate HTML for palette html = '