TaxonomyProject/SimulationImage
01.5k
1import sys, os
2from pathlib import Path
3import numpy as np
4from PIL import Image
5
6# ==== 可按需改动的常量 ====
7IMG_ROOT = "automatic_camera_test_demo"
8ANN_ROOT = "automatic_camera_test"
9TILE_WIDTH = 512 # 每张缩略图统一宽度
10PADDING = 8 # 小图之间的间距(像素)
11OUT_PATH = "thumb.png"
12
13def load_rgba(path: Path):
14 return Image.open(path).convert("RGBA") if path.exists() else None
15
16def resize_w(img: Image.Image, w: int):
17 if img is None: return None
18 ow, oh = img.size
19 if ow == w: return img
20 nh = int(round(oh * (w / ow)))
21 return img.resize((w, nh), Image.BICUBIC)
22
23def load_depth_as_img(npy_path: Path, width: int):
24 if not npy_path.exists(): return None
25 d = np.load(npy_path)
26 # 你的深度处理:clip 到 [0, 2048]
27 np.clip(d, 0.0, 2048.0, out=d)
28 # 线性映射到 0~255 灰度(2048 对应 255)
29 d = (d / 2048.0 * 255.0).astype(np.uint8)
30 img = Image.fromarray(d, mode="L").convert("RGBA")
31 return resize_w(img, width)
32
33def make_column(rgb, seg, depth, pad=PADDING):
34 tiles = [x for x in (rgb, seg, depth) if x is not None]
35 if not tiles: return None
36 w = max(t.size[0] for t in tiles)
37 h = sum(t.size[1] for t in tiles) + pad * (len(tiles) - 1)
38 col = Image.new("RGBA", (w, h), (255, 255, 255, 255))
39 y = 0
40 for t in tiles:
41 col.paste(t, (0, y))
42 y += t.size[1] + pad
43 return col
44
45def main():
46 if len(sys.argv) < 3:
47 print("用法: python make_thumbs_simple.py <scene_name> <pose1,pose2,...> [out.png]")
48 print("示例: python make_thumbs_simple.py Hospital_A 0001,0007,0042 thumbs.png")
49 sys.exit(1)
50
51 scene = sys.argv[1]
52 poses = [p.strip() for p in sys.argv[2].split(",") if p.strip()]
53 out_path = Path(sys.argv[3]) if len(sys.argv) >= 4 else Path(OUT_PATH)
54
55 columns = []
56 for pose in poses:
57 rgb_path = Path(IMG_ROOT) / scene / pose / "lit.png"
58 seg_path = Path(ANN_ROOT) / scene / pose / "seg.png"
59 depth_path = Path(ANN_ROOT) / scene / pose / "depth.npy"
60
61 rgb = resize_w(load_rgba(rgb_path), TILE_WIDTH)
62 seg = resize_w(load_rgba(seg_path), TILE_WIDTH)
63 depth = load_depth_as_img(depth_path, TILE_WIDTH)
64
65 col = make_column(rgb, seg, depth, PADDING)
66 if col is None:
67 print(f"[WARN] 跳过 pose {pose}: 三种文件都缺失")
68 continue
69 columns.append(col)
70
71 if not columns:
72 print("[ERROR] 没有任何可用列,检查路径是否正确")
73 sys.exit(2)
74
75 total_w = sum(c.size[0] for c in columns) + PADDING * (len(columns) - 1)
76 max_h = max(c.size[1] for c in columns)
77 canvas = Image.new("RGBA", (total_w, max_h), (255, 255, 255, 255))
78
79 x = 0
80 for c in columns:
81 canvas.paste(c, (x, 0))
82 x += c.size[0] + PADDING
83
84 out_path.parent.mkdir(parents=True, exist_ok=True)
85 canvas.convert("RGB").save(out_path, "PNG")
86 print(f"[OK] 已保存: {out_path.resolve()}")
87
88if __name__ == "__main__":
89 main()