Upload code/prepare_images.py with huggingface_hub
Browse files- code/prepare_images.py +46 -0
code/prepare_images.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Prepare images for COLMAP SfM + 3DGS.
|
| 3 |
+
- Convert RGBA -> RGB on white background
|
| 4 |
+
- Resize so max dimension <= 1600 (COLMAP works better with reasonable res)
|
| 5 |
+
- Organize into per-part input folders: data/parts/<part>/input/*.png
|
| 6 |
+
"""
|
| 7 |
+
import os
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
SRC = "data/raw"
|
| 12 |
+
DST = "data/parts"
|
| 13 |
+
MAX_DIM = 1600
|
| 14 |
+
|
| 15 |
+
os.makedirs(DST, exist_ok=True)
|
| 16 |
+
|
| 17 |
+
for part in sorted(os.listdir(SRC)):
|
| 18 |
+
src_dir = os.path.join(SRC, part)
|
| 19 |
+
if not os.path.isdir(src_dir):
|
| 20 |
+
continue
|
| 21 |
+
dst_dir = os.path.join(DST, part, "input")
|
| 22 |
+
os.makedirs(dst_dir, exist_ok=True)
|
| 23 |
+
for fname in sorted(os.listdir(src_dir)):
|
| 24 |
+
if not fname.lower().endswith(".png"):
|
| 25 |
+
continue
|
| 26 |
+
im = Image.open(os.path.join(src_dir, fname)).convert("RGBA")
|
| 27 |
+
arr = np.array(im)
|
| 28 |
+
# composite over white background
|
| 29 |
+
alpha = arr[:, :, 3:4].astype(np.float32) / 255.0
|
| 30 |
+
rgb = arr[:, :, :3].astype(np.float32)
|
| 31 |
+
bg = np.array([255, 255, 255], dtype=np.float32)
|
| 32 |
+
composited = rgb * alpha + bg * (1 - alpha)
|
| 33 |
+
out = Image.fromarray(composited.astype(np.uint8), "RGB")
|
| 34 |
+
# resize
|
| 35 |
+
w, h = out.size
|
| 36 |
+
scale = MAX_DIM / max(w, h)
|
| 37 |
+
if scale < 1.0:
|
| 38 |
+
nw, nh = int(w * scale), int(h * scale)
|
| 39 |
+
out = out.resize((nw, nh), Image.LANCZOS)
|
| 40 |
+
# rename to simple sequential
|
| 41 |
+
idx = fname.split("_")[-1].split(".")[0]
|
| 42 |
+
out_name = f"{part}_{idx}.png"
|
| 43 |
+
out.save(os.path.join(dst_dir, out_name))
|
| 44 |
+
print(f"{part}/{fname} -> {out_name} {out.size}")
|
| 45 |
+
|
| 46 |
+
print("Done preparing images.")
|