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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageDraw
|
| 3 |
+
import numpy as np
|
| 4 |
+
import math
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from itertools import cycle
|
| 7 |
+
from skimage.metrics import structural_similarity as ssim
|
| 8 |
+
from skimage.metrics import mean_squared_error
|
| 9 |
+
|
| 10 |
+
def mosaic_colour(img_array, width: int, height: int, length: int):
|
| 11 |
+
"""
|
| 12 |
+
Apply pixelated block mosaic to an RGB image array.
|
| 13 |
+
|
| 14 |
+
Divides the image into non-overlapping rectangular blocks of
|
| 15 |
+
size up to 'length * length',
|
| 16 |
+
Computes the mean RGB value of each block,
|
| 17 |
+
and fills that block with the averaged color.
|
| 18 |
+
"""
|
| 19 |
+
result = img_array.copy()
|
| 20 |
+
|
| 21 |
+
m, n = math.ceil(width / length), math.ceil(height / length)
|
| 22 |
+
for i in range(m):
|
| 23 |
+
for j in range(n):
|
| 24 |
+
left = i * length
|
| 25 |
+
right = min((i + 1) * length, width)
|
| 26 |
+
bottom = j * length
|
| 27 |
+
top = min((j + 1) * length, height)
|
| 28 |
+
rgb_avg = img_array[bottom:top, left:right, :].mean(axis=(0, 1))
|
| 29 |
+
result[bottom:top, left:right, 0] = round(rgb_avg[0])
|
| 30 |
+
result[bottom:top, left:right, 1] = round(rgb_avg[1])
|
| 31 |
+
result[bottom:top, left:right, 2] = round(rgb_avg[2])
|
| 32 |
+
|
| 33 |
+
return result
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def adjust_average_tone(image: Image.Image, target_mean=(128, 128, 128)) -> Image.Image:
|
| 37 |
+
"""
|
| 38 |
+
Globally shift an image's average color toward a target mean.
|
| 39 |
+
|
| 40 |
+
Performs a per-channel multiplicative scaling so that the mean of the
|
| 41 |
+
resulting image approximately equals `target_mean`. Values are clipped
|
| 42 |
+
into [0, 255].
|
| 43 |
+
"""
|
| 44 |
+
arr = np.array(image)
|
| 45 |
+
current_mean = arr.mean(axis=(0, 1))
|
| 46 |
+
|
| 47 |
+
scale = np.array(target_mean) / (current_mean + 1e-5)
|
| 48 |
+
new_arr = arr * scale
|
| 49 |
+
|
| 50 |
+
new_arr = np.clip(new_arr, 0, 255).astype(np.uint8)
|
| 51 |
+
|
| 52 |
+
return new_arr
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def mosaic_tile(img_array, width: int, height: int, length: int, tile_folder):
|
| 56 |
+
"""
|
| 57 |
+
Apply a tile-based mosaic using images from tile_images folder.
|
| 58 |
+
|
| 59 |
+
The image is divided into blocks of size up to 'length * length'.
|
| 60 |
+
For each block, this function selects the next tile image from 'tile_images' folder
|
| 61 |
+
(cycled), resizes it to the block size, adjusts its global average color
|
| 62 |
+
to match the block's average, and places it in the output.
|
| 63 |
+
"""
|
| 64 |
+
result = img_array.copy()
|
| 65 |
+
|
| 66 |
+
tile_iter = cycle(tile_folder.glob("*.jpg"))
|
| 67 |
+
|
| 68 |
+
m, n = math.ceil(width / length), math.ceil(height / length)
|
| 69 |
+
for i in range(m):
|
| 70 |
+
for j in range(n):
|
| 71 |
+
left = i * length
|
| 72 |
+
right = min((i + 1) * length, width)
|
| 73 |
+
bottom = j * length
|
| 74 |
+
top = min((j + 1) * length, height)
|
| 75 |
+
rgb_avg = img_array[bottom:top, left:right, :].mean(axis=(0, 1))
|
| 76 |
+
|
| 77 |
+
tile = Image.open(next(tile_iter))
|
| 78 |
+
new_tile_size = (right - left, top - bottom)
|
| 79 |
+
resized_tile = tile.resize(new_tile_size)
|
| 80 |
+
tile_array = adjust_average_tone(resized_tile, target_mean=rgb_avg)
|
| 81 |
+
|
| 82 |
+
result[bottom:top, left:right, :] = tile_array
|
| 83 |
+
|
| 84 |
+
return result
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def draw_grid(image: Image.Image, box_size: int, color=(0,0,0), width=1) -> Image.Image:
|
| 88 |
+
"""
|
| 89 |
+
Draws grid lines on top of an image to visualize segmentation.
|
| 90 |
+
"""
|
| 91 |
+
img_with_grid = image.copy()
|
| 92 |
+
draw = ImageDraw.Draw(img_with_grid)
|
| 93 |
+
w, h = img_with_grid.size
|
| 94 |
+
# vertical lines
|
| 95 |
+
for x in range(0, w, box_size):
|
| 96 |
+
draw.line([(x, 0), (x, h)], fill=color, width=width)
|
| 97 |
+
# horizontal lines
|
| 98 |
+
for y in range(0, h, box_size):
|
| 99 |
+
draw.line([(0, y), (w, y)], fill=color, width=width)
|
| 100 |
+
return img_with_grid
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def image_processing(input_image, Quantization: bool, Tiles: bool, resolution, box_size):
|
| 104 |
+
image = Image.fromarray(input_image)
|
| 105 |
+
|
| 106 |
+
# Quantization
|
| 107 |
+
if Quantization:
|
| 108 |
+
image = image.quantize()
|
| 109 |
+
image = image.convert("RGB")
|
| 110 |
+
|
| 111 |
+
# Resize
|
| 112 |
+
if resolution != "Original":
|
| 113 |
+
resolutions = resolution.split('×')
|
| 114 |
+
width, height = int(resolutions[0]), int(resolutions[1])
|
| 115 |
+
new_size = (width, height)
|
| 116 |
+
resized_image = image.resize(new_size)
|
| 117 |
+
else:
|
| 118 |
+
width, height = image.size[0], image.size[1]
|
| 119 |
+
resized_image = image
|
| 120 |
+
|
| 121 |
+
# Resized image with grid
|
| 122 |
+
segmented_image = draw_grid(resized_image, box_size)
|
| 123 |
+
|
| 124 |
+
# Mosaic
|
| 125 |
+
img_array = np.array(resized_image)
|
| 126 |
+
|
| 127 |
+
if Tiles:
|
| 128 |
+
folder = Path("tile_images")
|
| 129 |
+
img_array_mosaic = mosaic_tile(img_array, width, height, box_size, folder)
|
| 130 |
+
else:
|
| 131 |
+
img_array_mosaic = mosaic_colour(img_array, width, height, box_size)
|
| 132 |
+
|
| 133 |
+
# Performance Merics
|
| 134 |
+
metrics = calculate_metrics(img_array, img_array_mosaic)
|
| 135 |
+
metrics_text = f"MSE: {metrics[0]:.2f}\nSSIM: {metrics[1]:.2f}\nPSNR: {metrics[2]:.2f}\n"
|
| 136 |
+
|
| 137 |
+
return resized_image, segmented_image, Image.fromarray(img_array_mosaic), metrics_text
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def calculate_metrics(resized_image, mosaic_image):
|
| 141 |
+
"""Compute image quality metrics between a resized and mosaic image.
|
| 142 |
+
|
| 143 |
+
Calculates:
|
| 144 |
+
- MSE (Mean Squared Error)
|
| 145 |
+
- SSIM (Structural Similarity Index)
|
| 146 |
+
- PSNR (Peak Signal-to-Noise Ratio, in dB)
|
| 147 |
+
|
| 148 |
+
Args:
|
| 149 |
+
resized_image (np.ndarray): Reference RGB image array (uint8, H×W×3).
|
| 150 |
+
mosaic_image (np.ndarray): Processed RGB image array (uint8, H×W×3).
|
| 151 |
+
|
| 152 |
+
Returns:
|
| 153 |
+
list[float, float, float]: [mse, ssim_score, psnr_db].
|
| 154 |
+
"""
|
| 155 |
+
# MSE
|
| 156 |
+
mse = mean_squared_error(resized_image, mosaic_image)
|
| 157 |
+
|
| 158 |
+
# SSIM
|
| 159 |
+
ssim_score = ssim(resized_image, mosaic_image, channel_axis=2, data_range=255)
|
| 160 |
+
|
| 161 |
+
# PSNR (Peak Signal-to-Noise Ratio)
|
| 162 |
+
if mse == 0:
|
| 163 |
+
psnr = float('inf')
|
| 164 |
+
else:
|
| 165 |
+
psnr = 20 * np.log10(255.0 / np.sqrt(mse))
|
| 166 |
+
|
| 167 |
+
return [mse, ssim_score, psnr]
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
# Main
|
| 171 |
+
demo = gr.Interface(fn=image_processing,
|
| 172 |
+
inputs=[
|
| 173 |
+
gr.Image(label="Input Image"),
|
| 174 |
+
gr.Checkbox(),
|
| 175 |
+
gr.Checkbox(),
|
| 176 |
+
gr.Dropdown(
|
| 177 |
+
[
|
| 178 |
+
"Original",
|
| 179 |
+
"640×360",
|
| 180 |
+
"640×480",
|
| 181 |
+
"480×600",
|
| 182 |
+
"600×480",
|
| 183 |
+
"800×600",
|
| 184 |
+
"960×540",
|
| 185 |
+
"720×720",
|
| 186 |
+
"1024×768",
|
| 187 |
+
"960×960",
|
| 188 |
+
"1280×720",
|
| 189 |
+
"1024×1024"
|
| 190 |
+
],
|
| 191 |
+
label="Resolution",
|
| 192 |
+
info="Select the resolution you wish to use."
|
| 193 |
+
),
|
| 194 |
+
gr.Slider(1, 50, step=1, value=10, label="Box Size", info="Choose between 1 and 50"),
|
| 195 |
+
],
|
| 196 |
+
outputs=[
|
| 197 |
+
gr.Image(label="Resized Image"),
|
| 198 |
+
gr.Image(label="Segmented Image"),
|
| 199 |
+
gr.Image(label="Mosaic Image"),
|
| 200 |
+
gr.Textbox(label="Performance Metrics", lines=3)
|
| 201 |
+
],
|
| 202 |
+
examples=[
|
| 203 |
+
["imgs/portrait_1.jpg", True, True, "Original", 10],
|
| 204 |
+
["imgs/portrait_2.jpg", False, False, "480×600", 15],
|
| 205 |
+
["imgs/portrait_3.jpg", False, True, "720×720", 20],
|
| 206 |
+
["imgs/landscape_1.jpg", True, True, "640×360", 8],
|
| 207 |
+
["imgs/landscape_2.jpg", False, False, "960×540", 10],
|
| 208 |
+
["imgs/animal_1.jpg", False, True, "720×720", 10],
|
| 209 |
+
["imgs/animal_2.jpg", True, False, "720×720", 12],
|
| 210 |
+
["imgs/abstract_1.jpg", True, True, "640×360", 8],
|
| 211 |
+
["imgs/abstract_2.jpg", False, False, "640×480", 10],
|
| 212 |
+
["imgs/art_1.jpg", False, True, "640×480", 7]
|
| 213 |
+
])
|
| 214 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
Pillow
|
| 3 |
+
numpy
|
| 4 |
+
scikit-image
|