Datasets:
Upload scripts/stitch_squares.py with huggingface_hub
Browse files- scripts/stitch_squares.py +92 -0
scripts/stitch_squares.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
stitch_squares.py
|
| 3 |
+
=================
|
| 4 |
+
Builds the seven pre-defined VLM-friendly composite squares (two 3×3 and
|
| 5 |
+
five 2×2) from Gargano Sentinel-2 tiles in a folder.
|
| 6 |
+
|
| 7 |
+
The two land-free water tiles (r04c06, r04c07) are intentionally not covered.
|
| 8 |
+
|
| 9 |
+
Tile filenames must follow the dataset convention:
|
| 10 |
+
<prefix>_r<rr>_c<cc>.<ext>
|
| 11 |
+
|
| 12 |
+
Tiles are grouped by <prefix>; each group produces seven outputs.
|
| 13 |
+
|
| 14 |
+
Usage
|
| 15 |
+
-----
|
| 16 |
+
python stitch_squares.py <input_folder> [output_folder]
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
from __future__ import annotations
|
| 20 |
+
|
| 21 |
+
import re
|
| 22 |
+
import sys
|
| 23 |
+
from collections import defaultdict
|
| 24 |
+
from pathlib import Path
|
| 25 |
+
|
| 26 |
+
from PIL import Image
|
| 27 |
+
|
| 28 |
+
# (row_start, col_start, height, width)
|
| 29 |
+
SQUARES: list[tuple[int, int, int, int]] = [
|
| 30 |
+
(0, 0, 3, 3), # 3x3 r00-02 c00-02
|
| 31 |
+
(0, 3, 3, 3), # 3x3 r00-02 c03-05
|
| 32 |
+
(0, 6, 2, 2), # 2x2 r00-01 c06-07
|
| 33 |
+
(2, 6, 2, 2), # 2x2 r02-03 c06-07
|
| 34 |
+
(3, 0, 2, 2), # 2x2 r03-04 c00-01
|
| 35 |
+
(3, 2, 2, 2), # 2x2 r03-04 c02-03
|
| 36 |
+
(3, 4, 2, 2), # 2x2 r03-04 c04-05
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
PATTERN = re.compile(
|
| 40 |
+
r"^(?P<prefix>.+)_r(?P<row>\d{2})_c(?P<col>\d{2})\.(?P<ext>png|jpg|jpeg|tif|tiff)$",
|
| 41 |
+
re.IGNORECASE,
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def square_name(r0: int, c0: int, h: int, w: int) -> str:
|
| 46 |
+
return f"{h}x{w}_r{r0:02d}-{r0 + h - 1:02d}_c{c0:02d}-{c0 + w - 1:02d}"
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def main() -> None:
|
| 50 |
+
if len(sys.argv) < 2:
|
| 51 |
+
print("Usage: python stitch_squares.py <input_folder> [output_folder]")
|
| 52 |
+
sys.exit(1)
|
| 53 |
+
|
| 54 |
+
in_dir = Path(sys.argv[1])
|
| 55 |
+
out_dir = Path(sys.argv[2]) if len(sys.argv) > 2 else in_dir / "stitched_squares"
|
| 56 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 57 |
+
|
| 58 |
+
groups: dict[str, dict[tuple[int, int], Path]] = defaultdict(dict)
|
| 59 |
+
for p in sorted(in_dir.iterdir()):
|
| 60 |
+
m = PATTERN.match(p.name)
|
| 61 |
+
if not m:
|
| 62 |
+
continue
|
| 63 |
+
groups[m["prefix"]][(int(m["row"]), int(m["col"]))] = p
|
| 64 |
+
|
| 65 |
+
if not groups:
|
| 66 |
+
print(f"No tiles matching <prefix>_rNN_cNN.<ext> in {in_dir}")
|
| 67 |
+
return
|
| 68 |
+
|
| 69 |
+
for prefix, tiles in groups.items():
|
| 70 |
+
with Image.open(next(iter(tiles.values()))) as first:
|
| 71 |
+
tw, th = first.size
|
| 72 |
+
|
| 73 |
+
for r0, c0, h, w in SQUARES:
|
| 74 |
+
canvas = Image.new("RGB", (tw * w, th * h))
|
| 75 |
+
missing = 0
|
| 76 |
+
for dr in range(h):
|
| 77 |
+
for dc in range(w):
|
| 78 |
+
tp = tiles.get((r0 + dr, c0 + dc))
|
| 79 |
+
if tp is None:
|
| 80 |
+
missing += 1
|
| 81 |
+
continue
|
| 82 |
+
with Image.open(tp) as t:
|
| 83 |
+
canvas.paste(t.convert("RGB"), (dc * tw, dr * th))
|
| 84 |
+
|
| 85 |
+
out_path = out_dir / f"{prefix}_{square_name(r0, c0, h, w)}.png"
|
| 86 |
+
canvas.save(out_path)
|
| 87 |
+
flag = f" (missing {missing})" if missing else ""
|
| 88 |
+
print(f" → {out_path.name}{flag}")
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
main()
|