Spaces:
Running
Running
added clustering
Browse files
app.py
CHANGED
|
@@ -1,8 +1,51 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
def pixelate(img, pixel_size: int, blur=False):
|
| 6 |
if blur:
|
| 7 |
img = cv2.blur(img, (7, 7))
|
| 8 |
|
|
@@ -10,24 +53,50 @@ def pixelate(img, pixel_size: int, blur=False):
|
|
| 10 |
for j in range(0, img.shape[1], pixel_size):
|
| 11 |
img[i : i + pixel_size, j : j + pixel_size] = img[i][j]
|
| 12 |
|
| 13 |
-
return img
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
with gr.Blocks() as demo:
|
| 17 |
gr.Markdown("# Simple Pixelart Filter")
|
| 18 |
-
|
|
|
|
| 19 |
with gr.Column(variant="panel"):
|
| 20 |
img = gr.Image(label="Input Image")
|
| 21 |
pixel_size = gr.Number(label="Pixel Size", minimum=1, value=16)
|
| 22 |
blur = gr.Checkbox(label="Blur")
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
label="Output Image", format="jpeg", show_share_button=True
|
| 26 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
btn_clear = gr.ClearButton(components=[img, blur, output])
|
| 32 |
|
| 33 |
demo.launch(debug=True, pwa=True)
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def sort_by_brightness(palette: np.uint8):
|
| 7 |
+
# https://stackoverflow.com/a/596241
|
| 8 |
+
luminosity = (
|
| 9 |
+
0.2126 * palette[:, 2] + 0.7152 * palette[:, 1] + 0.0722 * palette[:, 0]
|
| 10 |
+
)
|
| 11 |
+
return palette[np.argsort(luminosity)]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def display_palette(palette: np.uint8, sort=True):
|
| 15 |
+
swatch_size = 100
|
| 16 |
+
num_colors = palette.shape[0]
|
| 17 |
+
palette_image = np.zeros((swatch_size, swatch_size * num_colors, 3), dtype=np.uint8)
|
| 18 |
+
|
| 19 |
+
if sort:
|
| 20 |
+
palette = sort_by_brightness(palette)
|
| 21 |
+
|
| 22 |
+
for i, color in enumerate(palette):
|
| 23 |
+
palette_image[:, i * swatch_size : (i + 1) * swatch_size] = color
|
| 24 |
+
|
| 25 |
+
return palette_image
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def extract_color_palette(img, k: int):
|
| 29 |
+
pixels = img.reshape((-1, 3))
|
| 30 |
+
pixels = np.float32(pixels)
|
| 31 |
|
| 32 |
+
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
|
| 33 |
+
_, labels, centers = cv2.kmeans(
|
| 34 |
+
pixels, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
palette = np.uint8(centers)
|
| 38 |
+
return palette, labels
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def pixelate(img, pixel_size: int, blur=False, use_palette=False, k=8):
|
| 42 |
+
palette = None
|
| 43 |
+
if use_palette:
|
| 44 |
+
palette, labels = extract_color_palette(img, k)
|
| 45 |
+
res = palette[labels.flatten()]
|
| 46 |
+
img = res.reshape((img.shape))
|
| 47 |
+
palette = display_palette(palette, sort=True)
|
| 48 |
|
|
|
|
| 49 |
if blur:
|
| 50 |
img = cv2.blur(img, (7, 7))
|
| 51 |
|
|
|
|
| 53 |
for j in range(0, img.shape[1], pixel_size):
|
| 54 |
img[i : i + pixel_size, j : j + pixel_size] = img[i][j]
|
| 55 |
|
| 56 |
+
return img, palette
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def update_palette_visibility(use_palette):
|
| 60 |
+
return gr.update(visible=use_palette), gr.update(visible=use_palette)
|
| 61 |
|
| 62 |
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
gr.Markdown("# Simple Pixelart Filter")
|
| 65 |
+
|
| 66 |
+
with gr.Row(equal_height=True):
|
| 67 |
with gr.Column(variant="panel"):
|
| 68 |
img = gr.Image(label="Input Image")
|
| 69 |
pixel_size = gr.Number(label="Pixel Size", minimum=1, value=16)
|
| 70 |
blur = gr.Checkbox(label="Blur")
|
| 71 |
+
use_palette = gr.Checkbox(label="Use Palette")
|
| 72 |
+
k = gr.Number(label="Number of Colours", minimum=2, value=8, visible=False)
|
| 73 |
+
|
| 74 |
+
with gr.Column(variant="panel"):
|
| 75 |
+
output_img = gr.Image(
|
| 76 |
label="Output Image", format="jpeg", show_share_button=True
|
| 77 |
)
|
| 78 |
+
output_palette = gr.Image(
|
| 79 |
+
label="Image Palette",
|
| 80 |
+
show_download_button=False,
|
| 81 |
+
show_share_button=False,
|
| 82 |
+
visible=False,
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
use_palette.change(
|
| 86 |
+
fn=update_palette_visibility,
|
| 87 |
+
inputs=use_palette,
|
| 88 |
+
outputs=[output_palette, k],
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
btn = gr.Button("Pixelate", variant="primary")
|
| 92 |
+
btn.click(
|
| 93 |
+
fn=pixelate,
|
| 94 |
+
inputs=[img, pixel_size, blur, use_palette, k],
|
| 95 |
+
outputs=[output_img, output_palette],
|
| 96 |
+
)
|
| 97 |
|
| 98 |
+
btn_clear = gr.ClearButton(
|
| 99 |
+
components=[img, blur, output_img, output_palette, use_palette]
|
| 100 |
+
)
|
|
|
|
| 101 |
|
| 102 |
demo.launch(debug=True, pwa=True)
|
nb.ipynb
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|