initialize mosaic with manual adjust grid size
Browse files- .DS_Store +0 -0
- .gitattributes +5 -0
- app.py +7 -0
- out/mosaic_avg_32.png +3 -0
- out/mosaic_palette_16_32.png +3 -0
- samples/.DS_Store +0 -0
- samples/IMG_3378.JPG +3 -0
- samples/IMG_5090.JPG +3 -0
- samples/IMG_6914.jpeg +3 -0
- simple_mosaic.py +92 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
.gitattributes
CHANGED
|
@@ -33,3 +33,8 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
out/*.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
samples/*.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
samples/*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
samples/*.JPG filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
samples/*.jpeg filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
def greet(name):
|
| 4 |
+
return "Hello " + name + "!!"
|
| 5 |
+
|
| 6 |
+
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
+
demo.launch()
|
out/mosaic_avg_32.png
ADDED
|
Git LFS Details
|
out/mosaic_palette_16_32.png
ADDED
|
Git LFS Details
|
samples/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
samples/IMG_3378.JPG
ADDED
|
|
Git LFS Details
|
samples/IMG_5090.JPG
ADDED
|
|
Git LFS Details
|
samples/IMG_6914.jpeg
ADDED
|
Git LFS Details
|
simple_mosaic.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# simple_mosaic.py
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from typing import List, Tuple, Iterator
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
class SimpleMosaicImage:
|
| 8 |
+
def __init__(self, path: str):
|
| 9 |
+
self.path = path
|
| 10 |
+
self.img = Image.open(path).convert("RGB")
|
| 11 |
+
self.width, self.height = self.img.size
|
| 12 |
+
print(f"[INFO] Loaded: {path} | size={self.width}x{self.height}")
|
| 13 |
+
|
| 14 |
+
def resize(self, longest_side: int = 512) -> "SimpleMosaicImage":
|
| 15 |
+
w, h = self.width, self.height
|
| 16 |
+
scale = longest_side / max(w, h)
|
| 17 |
+
if scale < 1.0:
|
| 18 |
+
new_w = int(w * scale)
|
| 19 |
+
new_h = int(h * scale)
|
| 20 |
+
self.img = self.img.resize((new_w, new_h), Image.BICUBIC)
|
| 21 |
+
self.width, self.height = new_w, new_h
|
| 22 |
+
print(f"[INFO] Resized to {new_w}x{new_h}")
|
| 23 |
+
return self
|
| 24 |
+
|
| 25 |
+
def crop_to_grid(self, grid_size: int = 32) -> "SimpleMosaicImage":
|
| 26 |
+
new_w = (self.width // grid_size) * grid_size
|
| 27 |
+
new_h = (self.height // grid_size) * grid_size
|
| 28 |
+
if new_w != self.width or new_h != self.height:
|
| 29 |
+
self.img = self.img.crop((0, 0, new_w, new_h))
|
| 30 |
+
self.width, self.height = new_w, new_h
|
| 31 |
+
print(f"[INFO] Cropped to {new_w}x{new_h} for grid {grid_size}")
|
| 32 |
+
return self
|
| 33 |
+
|
| 34 |
+
def _as_array(self):
|
| 35 |
+
return np.asarray(self.img, dtype=np.uint8)
|
| 36 |
+
|
| 37 |
+
def iter_cells(self, grid_size: int):
|
| 38 |
+
for y in range(0, self.height, grid_size):
|
| 39 |
+
for x in range(0, self.width, grid_size):
|
| 40 |
+
yield (x, y, grid_size, grid_size)
|
| 41 |
+
|
| 42 |
+
@staticmethod
|
| 43 |
+
def _cell_mean(arr, x, y, w, h):
|
| 44 |
+
block = arr[y:y+h, x:x+w, :]
|
| 45 |
+
mean = block.mean(axis=(0,1))
|
| 46 |
+
return tuple(int(round(v)) for v in mean)
|
| 47 |
+
|
| 48 |
+
@staticmethod
|
| 49 |
+
def _nearest_color(target, palette):
|
| 50 |
+
tr, tg, tb = target
|
| 51 |
+
pal = np.array(palette, dtype=np.int16)
|
| 52 |
+
diff = pal - np.array([tr, tg, tb], dtype=np.int16)
|
| 53 |
+
dist2 = np.sum(diff*diff, axis=1)
|
| 54 |
+
idx = int(np.argmin(dist2))
|
| 55 |
+
return tuple(int(v) for v in pal[idx])
|
| 56 |
+
|
| 57 |
+
def mosaic_average_color(self, grid_size: int = 32):
|
| 58 |
+
arr = self._as_array()
|
| 59 |
+
out = np.empty_like(arr)
|
| 60 |
+
for (x, y, w, h) in self.iter_cells(grid_size):
|
| 61 |
+
color = self._cell_mean(arr, x, y, w, h)
|
| 62 |
+
out[y:y+h, x:x+w, :] = color
|
| 63 |
+
return Image.fromarray(out, mode="RGB")
|
| 64 |
+
|
| 65 |
+
def mosaic_with_palette(self, grid_size: int, palette: List[Tuple[int,int,int]]):
|
| 66 |
+
arr = self._as_array()
|
| 67 |
+
out = np.empty_like(arr)
|
| 68 |
+
for (x, y, w, h) in self.iter_cells(grid_size):
|
| 69 |
+
avg = self._cell_mean(arr, x, y, w, h)
|
| 70 |
+
color = self._nearest_color(avg, palette)
|
| 71 |
+
out[y:y+h, x:x+w, :] = color
|
| 72 |
+
return Image.fromarray(out, mode="RGB")
|
| 73 |
+
|
| 74 |
+
def save(self, image: Image.Image, out_path: str) -> None:
|
| 75 |
+
Path(out_path).parent.mkdir(parents=True, exist_ok=True)
|
| 76 |
+
image.save(out_path)
|
| 77 |
+
print(f"[INFO] Saved: {out_path}")
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
PALETTE_16 = [
|
| 81 |
+
(0,0,0), (255,255,255), (255,0,0), (0,255,0), (0,0,255),
|
| 82 |
+
(255,255,0), (255,0,255), (0,255,255),
|
| 83 |
+
(128,128,128), (128,0,0), (0,128,0), (0,0,128),
|
| 84 |
+
(128,128,0), (128,0,128), (0,128,128), (200,200,200)
|
| 85 |
+
]
|
| 86 |
+
|
| 87 |
+
loader = SimpleMosaicImage("./samples/IMG_5090.jpg")
|
| 88 |
+
loader.resize(longest_side=512).crop_to_grid(grid_size=2)
|
| 89 |
+
|
| 90 |
+
mosaic = loader.mosaic_average_color(grid_size=2)
|
| 91 |
+
loader.save(mosaic, "./out/mosaic_avg_32.png")
|
| 92 |
+
|