Spaces:
Running
Running
add sorted color clusters
Browse files- myapp/app.py +3 -3
- myapp/palette.py +5 -1
myapp/app.py
CHANGED
|
@@ -10,7 +10,7 @@ from huggingface_hub import InferenceClient
|
|
| 10 |
from PIL import Image
|
| 11 |
from qrcode_artistic import write_artistic
|
| 12 |
|
| 13 |
-
from myapp.palette import array_to_hex, extract_color_clusters
|
| 14 |
|
| 15 |
try:
|
| 16 |
import dotenv
|
|
@@ -46,7 +46,7 @@ with gr.Blocks() as demo:
|
|
| 46 |
color_light = gr.ColorPicker("#FFFFFF", label="Light")
|
| 47 |
|
| 48 |
with gr.Row():
|
| 49 |
-
gr.ClearButton([color_dark, color_light])
|
| 50 |
extract_colors = gr.Button("Extract")
|
| 51 |
|
| 52 |
def generate_background(data: dict[Component, Any]):
|
|
@@ -94,7 +94,7 @@ with gr.Blocks() as demo:
|
|
| 94 |
|
| 95 |
image = Image.open(data[background])
|
| 96 |
k_means = extract_color_clusters(image, n_clusters=2)
|
| 97 |
-
primary, secondary = map(array_to_hex, k_means
|
| 98 |
|
| 99 |
return primary, secondary
|
| 100 |
|
|
|
|
| 10 |
from PIL import Image
|
| 11 |
from qrcode_artistic import write_artistic
|
| 12 |
|
| 13 |
+
from myapp.palette import array_to_hex, extract_color_clusters, sort_color_clusters
|
| 14 |
|
| 15 |
try:
|
| 16 |
import dotenv
|
|
|
|
| 46 |
color_light = gr.ColorPicker("#FFFFFF", label="Light")
|
| 47 |
|
| 48 |
with gr.Row():
|
| 49 |
+
gr.ClearButton([color_dark, color_light], value="Reset")
|
| 50 |
extract_colors = gr.Button("Extract")
|
| 51 |
|
| 52 |
def generate_background(data: dict[Component, Any]):
|
|
|
|
| 94 |
|
| 95 |
image = Image.open(data[background])
|
| 96 |
k_means = extract_color_clusters(image, n_clusters=2)
|
| 97 |
+
primary, secondary = map(array_to_hex, sort_color_clusters(k_means))
|
| 98 |
|
| 99 |
return primary, secondary
|
| 100 |
|
myapp/palette.py
CHANGED
|
@@ -27,8 +27,12 @@ def extract_color_clusters(image_array: np.ndarray | Image.Image, n_clusters=2):
|
|
| 27 |
return KMeans(n_clusters=n_clusters).fit(pixels)
|
| 28 |
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def iter_color_shades(k_means: KMeans, shades: tuple[float, ...]):
|
| 31 |
-
cluster_centers =
|
| 32 |
|
| 33 |
for delta, cluster_center in itertools.product(shades, cluster_centers):
|
| 34 |
yield add_hsv_saturation(cluster_center, delta)
|
|
|
|
| 27 |
return KMeans(n_clusters=n_clusters).fit(pixels)
|
| 28 |
|
| 29 |
|
| 30 |
+
def sort_color_clusters(k_means: KMeans):
|
| 31 |
+
return sorted(k_means.cluster_centers_, key=get_hsv_value)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
def iter_color_shades(k_means: KMeans, shades: tuple[float, ...]):
|
| 35 |
+
cluster_centers = sort_color_clusters(k_means)
|
| 36 |
|
| 37 |
for delta, cluster_center in itertools.product(shades, cluster_centers):
|
| 38 |
yield add_hsv_saturation(cluster_center, delta)
|