import sys, os from pathlib import Path import numpy as np from PIL import Image # ==== 可按需改动的常量 ==== IMG_ROOT = "automatic_camera_test_demo" ANN_ROOT = "automatic_camera_test" TILE_WIDTH = 512 # 每张缩略图统一宽度 PADDING = 8 # 小图之间的间距(像素) OUT_PATH = "thumb.png" def load_rgba(path: Path): return Image.open(path).convert("RGBA") if path.exists() else None def resize_w(img: Image.Image, w: int): if img is None: return None ow, oh = img.size if ow == w: return img nh = int(round(oh * (w / ow))) return img.resize((w, nh), Image.BICUBIC) def load_depth_as_img(npy_path: Path, width: int): if not npy_path.exists(): return None d = np.load(npy_path) # 你的深度处理:clip 到 [0, 2048] np.clip(d, 0.0, 2048.0, out=d) # 线性映射到 0~255 灰度(2048 对应 255) d = (d / 2048.0 * 255.0).astype(np.uint8) img = Image.fromarray(d, mode="L").convert("RGBA") return resize_w(img, width) def make_column(rgb, seg, depth, pad=PADDING): tiles = [x for x in (rgb, seg, depth) if x is not None] if not tiles: return None w = max(t.size[0] for t in tiles) h = sum(t.size[1] for t in tiles) + pad * (len(tiles) - 1) col = Image.new("RGBA", (w, h), (255, 255, 255, 255)) y = 0 for t in tiles: col.paste(t, (0, y)) y += t.size[1] + pad return col def main(): if len(sys.argv) < 3: print("用法: python make_thumbs_simple.py [out.png]") print("示例: python make_thumbs_simple.py Hospital_A 0001,0007,0042 thumbs.png") sys.exit(1) scene = sys.argv[1] poses = [p.strip() for p in sys.argv[2].split(",") if p.strip()] out_path = Path(sys.argv[3]) if len(sys.argv) >= 4 else Path(OUT_PATH) columns = [] for pose in poses: rgb_path = Path(IMG_ROOT) / scene / pose / "lit.png" seg_path = Path(ANN_ROOT) / scene / pose / "seg.png" depth_path = Path(ANN_ROOT) / scene / pose / "depth.npy" rgb = resize_w(load_rgba(rgb_path), TILE_WIDTH) seg = resize_w(load_rgba(seg_path), TILE_WIDTH) depth = load_depth_as_img(depth_path, TILE_WIDTH) col = make_column(rgb, seg, depth, PADDING) if col is None: print(f"[WARN] 跳过 pose {pose}: 三种文件都缺失") continue columns.append(col) if not columns: print("[ERROR] 没有任何可用列,检查路径是否正确") sys.exit(2) total_w = sum(c.size[0] for c in columns) + PADDING * (len(columns) - 1) max_h = max(c.size[1] for c in columns) canvas = Image.new("RGBA", (total_w, max_h), (255, 255, 255, 255)) x = 0 for c in columns: canvas.paste(c, (x, 0)) x += c.size[0] + PADDING out_path.parent.mkdir(parents=True, exist_ok=True) canvas.convert("RGB").save(out_path, "PNG") print(f"[OK] 已保存: {out_path.resolve()}") if __name__ == "__main__": main()