""" 이미지를 cols×rows 타일로 잘라 파일로 저장하고, R{행}C{열} 라벨을 찍은 격자 오버레이도 생성. 사용법: python tools/cut_tiles.py --input <이미지> --cols 9 --rows 6 출력: output/tiles/<이미지명>/R1C1.jpg ... (원본 배율) output/tiles/<이미지명>_grid.jpg (격자 오버레이, 최대 4096px) """ import argparse from pathlib import Path import cv2 import numpy as np def main(): ap = argparse.ArgumentParser() ap.add_argument("--input", required=True) ap.add_argument("--outdir", default="output/tiles") ap.add_argument("--cols", type=int, default=9) ap.add_argument("--rows", type=int, default=6) args = ap.parse_args() img_path = Path(args.input) buf = np.fromfile(str(img_path), dtype=np.uint8) img = cv2.imdecode(buf, cv2.IMREAD_COLOR) if img is None: print(f"이미지 로드 실패: {img_path}") return H, W = img.shape[:2] base_w = W / args.cols base_h = H / args.rows print(f"이미지 {W}×{H} → {args.cols}×{args.rows} 타일 {base_w:.0f}×{base_h:.0f}") tile_dir = Path(args.outdir) / img_path.stem tile_dir.mkdir(parents=True, exist_ok=True) vis = img.copy() font_scale = base_h / 200.0 thickness = max(2, int(font_scale * 2)) for r in range(args.rows): for c in range(args.cols): x0, x1 = int(c * base_w), int((c + 1) * base_w) y0, y1 = int(r * base_h), int((r + 1) * base_h) name = f"R{r+1}C{c+1}" cv2.imencode(".jpg", img[y0:y1, x0:x1], [cv2.IMWRITE_JPEG_QUALITY, 95])[1].tofile( str(tile_dir / f"{name}.jpg")) cv2.rectangle(vis, (x0, y0), (x1, y1), (0, 200, 255), 4) (tw, th), _ = cv2.getTextSize(name, cv2.FONT_HERSHEY_SIMPLEX, font_scale, thickness) tx, ty = x0 + 12, y0 + th + 12 cv2.rectangle(vis, (tx - 6, ty - th - 6), (tx + tw + 6, ty + 8), (0, 0, 0), -1) cv2.putText(vis, name, (tx, ty), cv2.FONT_HERSHEY_SIMPLEX, font_scale, (0, 200, 255), thickness, cv2.LINE_AA) h, w = vis.shape[:2] if max(h, w) > 4096: s = 4096 / max(h, w) vis = cv2.resize(vis, (int(w * s), int(h * s))) grid_path = Path(args.outdir) / f"{img_path.stem}_grid.jpg" cv2.imencode(".jpg", vis, [cv2.IMWRITE_JPEG_QUALITY, 92])[1].tofile(str(grid_path)) print(f"타일 {args.cols * args.rows}개 → {tile_dir}") print(f"격자 오버레이 → {grid_path}") if __name__ == "__main__": main()