Datasets:
Upload scripts/stitch_full.py with huggingface_hub
Browse files- scripts/stitch_full.py +77 -0
scripts/stitch_full.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
stitch_full.py
|
| 3 |
+
==============
|
| 4 |
+
Stitches Gargano Sentinel-2 tiles in a folder into one full-resolution image
|
| 5 |
+
(no labels, no borders).
|
| 6 |
+
|
| 7 |
+
Tile filenames must follow the dataset convention:
|
| 8 |
+
<prefix>_r<rr>_c<cc>.<ext>
|
| 9 |
+
|
| 10 |
+
Tiles are grouped by <prefix>; each group produces one mosaic.
|
| 11 |
+
|
| 12 |
+
Usage
|
| 13 |
+
-----
|
| 14 |
+
python stitch_full.py <input_folder> [output_folder]
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
from __future__ import annotations
|
| 18 |
+
|
| 19 |
+
import re
|
| 20 |
+
import sys
|
| 21 |
+
from collections import defaultdict
|
| 22 |
+
from pathlib import Path
|
| 23 |
+
|
| 24 |
+
from PIL import Image
|
| 25 |
+
|
| 26 |
+
GRID_ROWS = 5 # r00..r04
|
| 27 |
+
GRID_COLS = 8 # c00..c07
|
| 28 |
+
|
| 29 |
+
PATTERN = re.compile(
|
| 30 |
+
r"^(?P<prefix>.+)_r(?P<row>\d{2})_c(?P<col>\d{2})\.(?P<ext>png|jpg|jpeg|tif|tiff)$",
|
| 31 |
+
re.IGNORECASE,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def main() -> None:
|
| 36 |
+
if len(sys.argv) < 2:
|
| 37 |
+
print("Usage: python stitch_full.py <input_folder> [output_folder]")
|
| 38 |
+
sys.exit(1)
|
| 39 |
+
|
| 40 |
+
in_dir = Path(sys.argv[1])
|
| 41 |
+
out_dir = Path(sys.argv[2]) if len(sys.argv) > 2 else in_dir / "stitched_full"
|
| 42 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 43 |
+
|
| 44 |
+
groups: dict[str, dict[tuple[int, int], Path]] = defaultdict(dict)
|
| 45 |
+
for p in sorted(in_dir.iterdir()):
|
| 46 |
+
m = PATTERN.match(p.name)
|
| 47 |
+
if not m:
|
| 48 |
+
continue
|
| 49 |
+
groups[m["prefix"]][(int(m["row"]), int(m["col"]))] = p
|
| 50 |
+
|
| 51 |
+
if not groups:
|
| 52 |
+
print(f"No tiles matching <prefix>_rNN_cNN.<ext> in {in_dir}")
|
| 53 |
+
return
|
| 54 |
+
|
| 55 |
+
for prefix, tiles in groups.items():
|
| 56 |
+
with Image.open(next(iter(tiles.values()))) as first:
|
| 57 |
+
tw, th = first.size
|
| 58 |
+
|
| 59 |
+
canvas = Image.new("RGB", (tw * GRID_COLS, th * GRID_ROWS))
|
| 60 |
+
missing = 0
|
| 61 |
+
for r in range(GRID_ROWS):
|
| 62 |
+
for c in range(GRID_COLS):
|
| 63 |
+
tp = tiles.get((r, c))
|
| 64 |
+
if tp is None:
|
| 65 |
+
missing += 1
|
| 66 |
+
continue
|
| 67 |
+
with Image.open(tp) as t:
|
| 68 |
+
canvas.paste(t.convert("RGB"), (c * tw, r * th))
|
| 69 |
+
|
| 70 |
+
out_path = out_dir / f"{prefix}_full.png"
|
| 71 |
+
canvas.save(out_path)
|
| 72 |
+
flag = f" (missing {missing})" if missing else ""
|
| 73 |
+
print(f" → {out_path.name} [{canvas.size[0]}×{canvas.size[1]}]{flag}")
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
+
main()
|