Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import numpy as np
|
|
4 |
import matplotlib.pyplot as plt
|
5 |
from sklearn.cluster import KMeans
|
6 |
from collections import Counter
|
|
|
7 |
|
8 |
# Function to extract dominant colors
|
9 |
def extract_colors(image, num_colors=5):
|
@@ -18,42 +19,63 @@ def extract_colors(image, num_colors=5):
|
|
18 |
kmeans.fit(pixels)
|
19 |
colors = kmeans.cluster_centers_.astype(int)
|
20 |
|
21 |
-
|
22 |
-
counts = Counter(kmeans.labels_)
|
23 |
-
sorted_colors = [colors[i] for i in np.argsort(list(counts.values()))[::-1]]
|
24 |
-
|
25 |
-
return sorted_colors
|
26 |
|
27 |
# Convert RGB to Hex
|
28 |
def rgb_to_hex(rgb):
|
29 |
return "#" + "".join(f"{int(c):02x}" for c in rgb)
|
30 |
|
31 |
-
#
|
32 |
-
def
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
# Create color swatches
|
37 |
fig, ax = plt.subplots(figsize=(8, 2))
|
38 |
-
|
|
|
39 |
ax.set_xticks([])
|
40 |
ax.set_yticks([])
|
41 |
|
42 |
-
return fig,
|
43 |
|
44 |
# Gradio UI
|
45 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
46 |
gr.Markdown("# π¨ AI-Powered Image Color Palette Generator")
|
47 |
-
gr.Markdown("Upload an image to extract its dominant colors
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
|
53 |
palette_output = gr.Plot(label="Color Palette")
|
54 |
-
hex_output = gr.Textbox(label="Hex Codes", interactive=
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
|
57 |
|
58 |
# Launch the app
|
59 |
if __name__ == "__main__":
|
|
|
4 |
import matplotlib.pyplot as plt
|
5 |
from sklearn.cluster import KMeans
|
6 |
from collections import Counter
|
7 |
+
import random
|
8 |
|
9 |
# Function to extract dominant colors
|
10 |
def extract_colors(image, num_colors=5):
|
|
|
19 |
kmeans.fit(pixels)
|
20 |
colors = kmeans.cluster_centers_.astype(int)
|
21 |
|
22 |
+
return colors
|
|
|
|
|
|
|
|
|
23 |
|
24 |
# Convert RGB to Hex
|
25 |
def rgb_to_hex(rgb):
|
26 |
return "#" + "".join(f"{int(c):02x}" for c in rgb)
|
27 |
|
28 |
+
# Generate AI-based harmonious color palettes
|
29 |
+
def generate_ai_palette():
|
30 |
+
return [rgb_to_hex([random.randint(0, 255) for _ in range(3)]) for _ in range(5)]
|
31 |
+
|
32 |
+
# Function to update palette with user selection
|
33 |
+
def update_palette(image, selected_x, selected_y, locked_colors):
|
34 |
+
if image is not None:
|
35 |
+
selected_color = image[int(selected_y), int(selected_x)]
|
36 |
+
selected_hex = rgb_to_hex(selected_color)
|
37 |
+
|
38 |
+
colors = extract_colors(image, 5)
|
39 |
+
hex_colors = [rgb_to_hex(color) for color in colors]
|
40 |
+
|
41 |
+
# Replace unlocked colors with extracted ones
|
42 |
+
for i in range(5):
|
43 |
+
if locked_colors[i] == "":
|
44 |
+
locked_colors[i] = selected_hex if i == 0 else hex_colors[i]
|
45 |
+
else:
|
46 |
+
# If no image, generate AI colors
|
47 |
+
locked_colors = [color if color != "" else rgb_to_hex([random.randint(0, 255) for _ in range(3)]) for color in locked_colors]
|
48 |
|
49 |
# Create color swatches
|
50 |
fig, ax = plt.subplots(figsize=(8, 2))
|
51 |
+
rgb_colors = [[int(h[i:i+2], 16) for i in (1, 3, 5)] for h in locked_colors]
|
52 |
+
ax.imshow([rgb_colors], aspect="auto")
|
53 |
ax.set_xticks([])
|
54 |
ax.set_yticks([])
|
55 |
|
56 |
+
return fig, locked_colors
|
57 |
|
58 |
# Gradio UI
|
59 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
60 |
gr.Markdown("# π¨ AI-Powered Image Color Palette Generator")
|
61 |
+
gr.Markdown("Upload an image to extract its dominant colors, or generate AI-based color palettes.")
|
62 |
+
|
63 |
+
image_input = gr.Image(type="numpy", label="Upload an Image (Optional)")
|
64 |
|
65 |
+
with gr.Row():
|
66 |
+
generate_ai_button = gr.Button("Generate AI Palette π¨")
|
67 |
+
regenerate_button = gr.Button("Re-run Unlocked Colors π")
|
68 |
|
69 |
palette_output = gr.Plot(label="Color Palette")
|
70 |
+
hex_output = gr.Textbox(label="Hex Codes", interactive=True)
|
71 |
+
|
72 |
+
with gr.Row():
|
73 |
+
locked_colors = [gr.Textbox(label=f"Color {i+1}", interactive=True) for i in range(5)]
|
74 |
+
|
75 |
+
generate_ai_button.click(generate_ai_palette, inputs=[], outputs=locked_colors)
|
76 |
+
regenerate_button.click(update_palette, inputs=[image_input, None, None, locked_colors], outputs=[palette_output, locked_colors])
|
77 |
|
78 |
+
image_input.select(update_palette, inputs=[image_input, image_input.select, image_input.select, locked_colors], outputs=[palette_output, locked_colors])
|
79 |
|
80 |
# Launch the app
|
81 |
if __name__ == "__main__":
|