Spaces:
Build error
Build error
dev(hansbug): update code
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
|
@@ -16,7 +17,8 @@ def _image_resize(image: Image.Image, pixels: int = 90000, **kwargs):
|
|
| 16 |
return small_image
|
| 17 |
|
| 18 |
|
| 19 |
-
def get_main_colors(image: Image.Image, n: int = 28, pixels: int = 90000)
|
|
|
|
| 20 |
image = image.copy()
|
| 21 |
if image.mode != 'RGB':
|
| 22 |
image = image.convert('RGB')
|
|
@@ -33,20 +35,27 @@ def get_main_colors(image: Image.Image, n: int = 28, pixels: int = 90000) -> Ima
|
|
| 33 |
new_data = colors[prediction].reshape((height, width, 3))
|
| 34 |
new_image = Image.fromarray(new_data, mode='RGB')
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
def main_func(image: Image.Image, n: int, pixels: int, fixed_width: bool, width: int):
|
| 40 |
-
new_image = get_main_colors(image, n, pixels)
|
| 41 |
if fixed_width:
|
| 42 |
-
_width, _height =
|
| 43 |
r = width / _width
|
| 44 |
new_width, new_height = int(round(_width * r)), int(round(_height * r))
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
table['ratio'] = table['count'] / table['count'].sum()
|
| 51 |
hexes = []
|
| 52 |
for r, g, b in zip(table['r'], table['g'], table['b']):
|
|
@@ -60,7 +69,7 @@ def main_func(image: Image.Image, n: int, pixels: int, fixed_width: bool, width:
|
|
| 60 |
'Red': table['r'],
|
| 61 |
'Green': table['g'],
|
| 62 |
'Blue': table['b'],
|
| 63 |
-
})
|
| 64 |
|
| 65 |
return new_image, new_table
|
| 66 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
from typing import Tuple
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
import numpy as np
|
|
|
|
| 17 |
return small_image
|
| 18 |
|
| 19 |
|
| 20 |
+
def get_main_colors(image: Image.Image, n: int = 28, pixels: int = 90000) \
|
| 21 |
+
-> Tuple[Image.Image, np.ndarray, np.ndarray, np.ndarray]:
|
| 22 |
image = image.copy()
|
| 23 |
if image.mode != 'RGB':
|
| 24 |
image = image.convert('RGB')
|
|
|
|
| 35 |
new_data = colors[prediction].reshape((height, width, 3))
|
| 36 |
new_image = Image.fromarray(new_data, mode='RGB')
|
| 37 |
|
| 38 |
+
cids, counts = np.unique(prediction, return_counts=True)
|
| 39 |
+
counts = np.asarray(list(map(lambda x: x[1], sorted(zip(cids, counts)))))
|
| 40 |
+
|
| 41 |
+
return new_image, colors, counts, prediction.reshape((height, width))
|
| 42 |
|
| 43 |
|
| 44 |
def main_func(image: Image.Image, n: int, pixels: int, fixed_width: bool, width: int):
|
|
|
|
| 45 |
if fixed_width:
|
| 46 |
+
_width, _height = image.size
|
| 47 |
r = width / _width
|
| 48 |
new_width, new_height = int(round(_width * r)), int(round(_height * r))
|
| 49 |
+
image = image.resize((new_width, new_height))
|
| 50 |
+
|
| 51 |
+
new_image, colors, counts, predictions = get_main_colors(image, n, pixels)
|
| 52 |
|
| 53 |
+
table = pd.DataFrame({
|
| 54 |
+
'r': colors[:, 0],
|
| 55 |
+
'g': colors[:, 1],
|
| 56 |
+
'b': colors[:, 2],
|
| 57 |
+
'count': counts,
|
| 58 |
+
})
|
| 59 |
table['ratio'] = table['count'] / table['count'].sum()
|
| 60 |
hexes = []
|
| 61 |
for r, g, b in zip(table['r'], table['g'], table['b']):
|
|
|
|
| 69 |
'Red': table['r'],
|
| 70 |
'Green': table['g'],
|
| 71 |
'Blue': table['b'],
|
| 72 |
+
}).sort_values('Pixels', ascending=False)
|
| 73 |
|
| 74 |
return new_image, new_table
|
| 75 |
|