m3g4p0p commited on
Commit
7a8f70c
·
1 Parent(s): 1ea41a7

add generate palette

Browse files
Files changed (1) hide show
  1. myapp/app.py +24 -2
myapp/app.py CHANGED
@@ -1,7 +1,7 @@
1
  import re
2
  from functools import partial
3
  from io import BytesIO
4
- from typing import Any
5
 
6
  import gradio as gr
7
  import segno
@@ -10,6 +10,8 @@ from huggingface_hub import InferenceClient
10
  from PIL import Image
11
  from qrcode_artistic import write_artistic
12
 
 
 
13
  try:
14
  import dotenv
15
 
@@ -54,6 +56,9 @@ with gr.Blocks() as demo:
54
  return None
55
 
56
  def to_hex_format(value: str):
 
 
 
57
  if value.startswith("#"):
58
  return value
59
 
@@ -69,15 +74,26 @@ with gr.Blocks() as demo:
69
  write_artistic(
70
  qr_code,
71
  target=buffer,
72
- background=image.fp,
73
  kind=image.format,
74
  scale=data[scale],
75
  light=to_hex_format(data[color_light]),
76
  dark=to_hex_format(data[color_dark]),
 
77
  )
78
 
79
  return Image.open(buffer)
80
 
 
 
 
 
 
 
 
 
 
 
81
  gr.on(
82
  [button.click, prompt.submit],
83
  partial(gr.update, interactive=False),
@@ -110,6 +126,12 @@ with gr.Blocks() as demo:
110
  outputs=output,
111
  )
112
 
 
 
 
 
 
 
113
 
114
  if __name__ == "__main__":
115
  demo.launch()
 
1
  import re
2
  from functools import partial
3
  from io import BytesIO
4
+ from typing import Any, cast
5
 
6
  import gradio as gr
7
  import segno
 
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
17
 
 
56
  return None
57
 
58
  def to_hex_format(value: str):
59
+ if value is None:
60
+ return None
61
+
62
  if value.startswith("#"):
63
  return value
64
 
 
74
  write_artistic(
75
  qr_code,
76
  target=buffer,
77
+ background=image.filename,
78
  kind=image.format,
79
  scale=data[scale],
80
  light=to_hex_format(data[color_light]),
81
  dark=to_hex_format(data[color_dark]),
82
+ quiet_zone=cast(Any, "#FFFFFF"),
83
  )
84
 
85
  return Image.open(buffer)
86
 
87
+ def generate_palette(data: dict[Component, Any]):
88
+ if data[background] is None:
89
+ return None, None
90
+
91
+ image = Image.open(data[background])
92
+ k_means = extract_color_clusters(image, n_clusters=2)
93
+ primary, secondary = map(array_to_hex, k_means.cluster_centers_)
94
+
95
+ return primary, secondary
96
+
97
  gr.on(
98
  [button.click, prompt.submit],
99
  partial(gr.update, interactive=False),
 
126
  outputs=output,
127
  )
128
 
129
+ gr.on(
130
+ fn=generate_palette,
131
+ inputs={background},
132
+ outputs=[color_dark, color_light],
133
+ )
134
+
135
 
136
  if __name__ == "__main__":
137
  demo.launch()