Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from sklearn.cluster import KMeans
|
| 5 |
+
|
| 6 |
+
def extract_colors(img, num_colors):
|
| 7 |
+
if img is None:
|
| 8 |
+
return "<p>No image uploaded.</p>"
|
| 9 |
+
|
| 10 |
+
# Resize image for faster processing
|
| 11 |
+
img_resized = img.resize((150, 150))
|
| 12 |
+
data = np.array(img_resized)
|
| 13 |
+
data = data.reshape(-1, 3)
|
| 14 |
+
|
| 15 |
+
# Remove any grayscale or alpha if present
|
| 16 |
+
if data.shape[1] == 4:
|
| 17 |
+
data = data[:, :3]
|
| 18 |
+
|
| 19 |
+
# Fit KMeans
|
| 20 |
+
kmeans = KMeans(n_clusters=num_colors, random_state=42)
|
| 21 |
+
kmeans.fit(data)
|
| 22 |
+
|
| 23 |
+
# Get cluster centers
|
| 24 |
+
colors = kmeans.cluster_centers_.round().astype(int)
|
| 25 |
+
|
| 26 |
+
# Convert to hex
|
| 27 |
+
hex_colors = ['#' + ''.join(f'{c:02x}' for c in color) for color in colors]
|
| 28 |
+
|
| 29 |
+
# Generate HTML for palette
|
| 30 |
+
html = '<div style="text-align: center;"><h2>Color Palette</h2><div style="display: flex; flex-wrap: wrap; justify-content: center; gap: 10px;">'
|
| 31 |
+
for hex_color in hex_colors:
|
| 32 |
+
html += f'''
|
| 33 |
+
<div style="background-color: {hex_color}; width: 80px; height: 80px; border: 2px solid #333; border-radius: 10px; display: flex; align-items: center; justify-content: center; color: {hex_color};">
|
| 34 |
+
<span style="background: white; padding: 2px 5px; border-radius: 3px; font-size: 12px; font-weight: bold;">{hex_color}</span>
|
| 35 |
+
</div>
|
| 36 |
+
'''
|
| 37 |
+
html += '</div></div>'
|
| 38 |
+
return html
|
| 39 |
+
|
| 40 |
+
with gr.Blocks(title="Image Color Palette Extractor") as demo:
|
| 41 |
+
gr.Markdown("# Image Color Palette Extractor")
|
| 42 |
+
gr.Markdown("Upload an image to extract the main colors and generate a palette.")
|
| 43 |
+
|
| 44 |
+
with gr.Row():
|
| 45 |
+
with gr.Column(scale=1):
|
| 46 |
+
input_img = gr.Image(type="pil", label="Upload Image")
|
| 47 |
+
num_colors = gr.Slider(2, 12, value=6, step=1, label="Number of Colors")
|
| 48 |
+
extract_btn = gr.Button("Extract Colors", variant="primary")
|
| 49 |
+
|
| 50 |
+
with gr.Column(scale=2):
|
| 51 |
+
palette_output = gr.HTML()
|
| 52 |
+
|
| 53 |
+
extract_btn.click(
|
| 54 |
+
extract_colors,
|
| 55 |
+
inputs=[input_img, num_colors],
|
| 56 |
+
outputs=palette_output
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
input_img.change(
|
| 60 |
+
extract_colors,
|
| 61 |
+
inputs=[input_img, num_colors],
|
| 62 |
+
outputs=palette_output
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
demo.queue()
|
| 66 |
+
demo.launch()
|