Spaces:
Running
Running
refactor
Browse files- .gitignore +2 -1
- README.md +2 -1
- app.py +2 -54
- example_images/bananas.jpg +0 -0
- src/pixelate.py +71 -0
.gitignore
CHANGED
|
@@ -2,4 +2,5 @@
|
|
| 2 |
.gradio/
|
| 3 |
.vscode/
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
| 2 |
.gradio/
|
| 3 |
.vscode/
|
| 4 |
|
| 5 |
+
__pycache__
|
| 6 |
+
imgs
|
README.md
CHANGED
|
@@ -9,4 +9,5 @@ app_file: app.py
|
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
# Simple Pixelart Filter
|
|
|
|
|
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# Simple Pixelart Filter
|
| 13 |
+
|
app.py
CHANGED
|
@@ -1,59 +1,6 @@
|
|
| 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 |
-
|
| 52 |
-
for i in range(0, img.shape[0], pixel_size):
|
| 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):
|
|
@@ -103,6 +50,7 @@ with gr.Blocks() as demo:
|
|
| 103 |
gr.Markdown("## Examples")
|
| 104 |
gr.Examples(
|
| 105 |
examples=[
|
|
|
|
| 106 |
["example_images/scream.jpg", 16, False, True, 4],
|
| 107 |
["example_images/cat.jpg", 32, True, False, None],
|
| 108 |
["example_images/ducks.jpg", 4, False, True, 2],
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
+
from src.pixelate import pixelate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
def update_palette_visibility(use_palette):
|
|
|
|
| 50 |
gr.Markdown("## Examples")
|
| 51 |
gr.Examples(
|
| 52 |
examples=[
|
| 53 |
+
["example_images/bananas.jpg", 16, False, True, 8],
|
| 54 |
["example_images/scream.jpg", 16, False, True, 4],
|
| 55 |
["example_images/cat.jpg", 32, True, False, None],
|
| 56 |
["example_images/ducks.jpg", 4, False, True, 2],
|
example_images/bananas.jpg
ADDED
|
|
src/pixelate.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def sort_by_brightness(palette: np.uint8):
|
| 6 |
+
"""
|
| 7 |
+
Sorts given color palette by brightness.
|
| 8 |
+
|
| 9 |
+
https://stackoverflow.com/a/596241
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
luminosity = (
|
| 13 |
+
0.2126 * palette[:, 2] + 0.7152 * palette[:, 1] + 0.0722 * palette[:, 0]
|
| 14 |
+
)
|
| 15 |
+
return palette[np.argsort(luminosity)]
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def display_palette(palette: np.uint8, sort=True):
|
| 19 |
+
"""
|
| 20 |
+
Generates an image displaying given color palette.
|
| 21 |
+
"""
|
| 22 |
+
swatch_size = 100
|
| 23 |
+
num_colors = palette.shape[0]
|
| 24 |
+
palette_image = np.zeros((swatch_size, swatch_size * num_colors, 3), dtype=np.uint8)
|
| 25 |
+
|
| 26 |
+
if sort:
|
| 27 |
+
palette = sort_by_brightness(palette)
|
| 28 |
+
|
| 29 |
+
for i, color in enumerate(palette):
|
| 30 |
+
palette_image[:, i * swatch_size : (i + 1) * swatch_size] = color
|
| 31 |
+
|
| 32 |
+
return palette_image
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def extract_color_palette(img, k: int):
|
| 36 |
+
"""
|
| 37 |
+
Extracts color palette from the given image using k-means clustering.
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
pixels = img.reshape((-1, 3))
|
| 41 |
+
pixels = np.float32(pixels)
|
| 42 |
+
|
| 43 |
+
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
|
| 44 |
+
_, labels, centers = cv2.kmeans(
|
| 45 |
+
pixels, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
palette = np.uint8(centers)
|
| 49 |
+
return palette, labels
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def pixelate(img, pixel_size: int, blur=False, use_palette=False, k=8):
|
| 53 |
+
"""
|
| 54 |
+
Pixelates an image by reducing its pixel resolution and optionally applying blur effect and color quantization.
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
palette = None
|
| 58 |
+
if use_palette:
|
| 59 |
+
palette, labels = extract_color_palette(img, k)
|
| 60 |
+
res = palette[labels.flatten()]
|
| 61 |
+
img = res.reshape((img.shape))
|
| 62 |
+
palette = display_palette(palette, sort=True)
|
| 63 |
+
|
| 64 |
+
if blur:
|
| 65 |
+
img = cv2.blur(img, (7, 7))
|
| 66 |
+
|
| 67 |
+
for i in range(0, img.shape[0], pixel_size):
|
| 68 |
+
for j in range(0, img.shape[1], pixel_size):
|
| 69 |
+
img[i : i + pixel_size, j : j + pixel_size] = img[i][j]
|
| 70 |
+
|
| 71 |
+
return img, palette
|