Spaces:
Build error
Build error
dev(narugo): save this
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
from sklearn.cluster import KMeans
|
| 7 |
|
|
@@ -27,10 +28,12 @@ def get_main_colors(image: Image.Image, n: int = 28, pixels: int = 90000) -> Ima
|
|
| 27 |
|
| 28 |
width, height = image.size
|
| 29 |
raw = np.asarray(image).reshape(-1, 3)
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
return
|
| 34 |
|
| 35 |
|
| 36 |
def main_func(image: Image.Image, n: int, pixels: int, fixed_width: bool, width: int):
|
|
@@ -41,10 +44,29 @@ def main_func(image: Image.Image, n: int, pixels: int, fixed_width: bool, width:
|
|
| 41 |
new_width, new_height = int(round(_width * r)), int(round(_height * r))
|
| 42 |
new_image = new_image.resize((new_width, new_height), resample=Image.NEAREST)
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
|
| 47 |
if __name__ == '__main__':
|
|
|
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
with gr.Row():
|
| 50 |
with gr.Column():
|
|
@@ -59,12 +81,19 @@ if __name__ == '__main__':
|
|
| 59 |
ch_submit = gr.Button(value='Submit', variant='primary')
|
| 60 |
|
| 61 |
with gr.Column():
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
ch_submit.click(
|
| 65 |
main_func,
|
| 66 |
inputs=[ch_image, ch_clusters, ch_pixels, ch_fixed_width, ch_width],
|
| 67 |
-
outputs=[ch_output],
|
| 68 |
)
|
| 69 |
|
| 70 |
demo.queue(os.cpu_count()).launch()
|
|
|
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
from PIL import Image
|
| 7 |
from sklearn.cluster import KMeans
|
| 8 |
|
|
|
|
| 28 |
|
| 29 |
width, height = image.size
|
| 30 |
raw = np.asarray(image).reshape(-1, 3)
|
| 31 |
+
colors = kmeans.cluster_centers_.round().astype(np.uint8)
|
| 32 |
+
prediction = kmeans.predict(raw)
|
| 33 |
+
new_data = colors[prediction].reshape((height, width, 3))
|
| 34 |
+
new_image = Image.fromarray(new_data, mode='RGB')
|
| 35 |
|
| 36 |
+
return new_image
|
| 37 |
|
| 38 |
|
| 39 |
def main_func(image: Image.Image, n: int, pixels: int, fixed_width: bool, width: int):
|
|
|
|
| 44 |
new_width, new_height = int(round(_width * r)), int(round(_height * r))
|
| 45 |
new_image = new_image.resize((new_width, new_height), resample=Image.NEAREST)
|
| 46 |
|
| 47 |
+
df = pd.DataFrame(data=np.asarray(new_image).reshape(-1, 3), columns=['r', 'g', 'b'])
|
| 48 |
+
df['id_'] = 1
|
| 49 |
+
table = df.groupby(['r', 'g', 'b'])['id_'].agg(['count']).reset_index().sort_values('count', ascending=False)
|
| 50 |
+
table['ratio'] = table['count'] / table['count'].sum()
|
| 51 |
+
hexes = []
|
| 52 |
+
for r, g, b in zip(table['r'], table['g'], table['b']):
|
| 53 |
+
hexes.append(f'#{r:02x}{g:02x}{b:02x}')
|
| 54 |
+
table['hex'] = hexes
|
| 55 |
+
|
| 56 |
+
new_table = pd.DataFrame({
|
| 57 |
+
'Hex': table['hex'],
|
| 58 |
+
'Pixels': table['count'],
|
| 59 |
+
'Ratio': table['ratio'],
|
| 60 |
+
'Red': table['r'],
|
| 61 |
+
'Green': table['g'],
|
| 62 |
+
'Blue': table['b'],
|
| 63 |
+
})
|
| 64 |
+
|
| 65 |
+
return new_image, new_table
|
| 66 |
|
| 67 |
|
| 68 |
if __name__ == '__main__':
|
| 69 |
+
pd.set_option("display.precision", 3)
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
with gr.Row():
|
| 72 |
with gr.Column():
|
|
|
|
| 81 |
ch_submit = gr.Button(value='Submit', variant='primary')
|
| 82 |
|
| 83 |
with gr.Column():
|
| 84 |
+
with gr.Tabs():
|
| 85 |
+
with gr.Tab('Output Image'):
|
| 86 |
+
ch_output = gr.Image(type='pil', label='Output Image')
|
| 87 |
+
with gr.Tab('Color Map'):
|
| 88 |
+
ch_color_map = gr.Dataframe(
|
| 89 |
+
headers=['Hex', 'Pixels', 'Ratio', 'Red', 'Green', 'Blue'],
|
| 90 |
+
label='Color Map'
|
| 91 |
+
)
|
| 92 |
|
| 93 |
ch_submit.click(
|
| 94 |
main_func,
|
| 95 |
inputs=[ch_image, ch_clusters, ch_pixels, ch_fixed_width, ch_width],
|
| 96 |
+
outputs=[ch_output, ch_color_map],
|
| 97 |
)
|
| 98 |
|
| 99 |
demo.queue(os.cpu_count()).launch()
|