VanKee commited on
Commit
b041b2b
·
1 Parent(s): 4bcf206

initialize mosaic with manual adjust grid size

Browse files
.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

  • SHA256: 021fa76347c86483a0d4e4364cad988f1fa7288246ddabc99a385bc41cc14771
  • Pointer size: 131 Bytes
  • Size of remote file: 118 kB
out/mosaic_palette_16_32.png ADDED

Git LFS Details

  • SHA256: 3eb6c8a0abd2e16d1bccd8cd20b29ed249d916a9db56107fc29e62d2b112dd63
  • Pointer size: 129 Bytes
  • Size of remote file: 1.81 kB
samples/.DS_Store ADDED
Binary file (6.15 kB). View file
 
samples/IMG_3378.JPG ADDED

Git LFS Details

  • SHA256: 692cc94e92d109e17270e5c1e3ff0de1b73ba47a9bda39a014a1b182e95b0eec
  • Pointer size: 132 Bytes
  • Size of remote file: 1.98 MB
samples/IMG_5090.JPG ADDED

Git LFS Details

  • SHA256: 5b9608958156659040bb106ca5361973a91dddca0b158a5a11ca6783d8e1463f
  • Pointer size: 132 Bytes
  • Size of remote file: 2.72 MB
samples/IMG_6914.jpeg ADDED

Git LFS Details

  • SHA256: ad88a2d9a2c09102d2ac3f9150d18977e0dab750b7656afb3c9c15bcf40cdd9f
  • Pointer size: 132 Bytes
  • Size of remote file: 1.27 MB
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
+