Skip to content

Commit 741d44b

Browse files
committed
helper function
1 parent 92e2640 commit 741d44b

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

examples/datasets/colmap.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,31 @@ def _get_rel_paths(path_dir: str) -> List[str]:
2828
return paths
2929

3030

31+
def _resize_image_folder(image_dir: str, resized_dir: str, factor: int) -> str:
32+
"""Resize image folder."""
33+
print("Downscaling full resolution images instead of provided jpgs.")
34+
os.makedirs(resized_dir, exist_ok=True)
35+
36+
image_files = _get_rel_paths(image_dir)
37+
for image_file in tqdm(image_files):
38+
image_path = os.path.join(image_dir, image_file)
39+
resized_path = os.path.join(
40+
resized_dir, os.path.splitext(image_file)[0] + ".png"
41+
)
42+
if os.path.isfile(resized_path):
43+
continue
44+
image = imageio.imread(image_path)[..., :3]
45+
resized_size = (
46+
int(round(image.shape[1] / factor)),
47+
int(round(image.shape[0] / factor)),
48+
)
49+
resized_image = np.array(
50+
Image.fromarray(image).resize(resized_size, Image.BICUBIC)
51+
)
52+
imageio.imwrite(resized_path, resized_image)
53+
return resized_dir
54+
55+
3156
class Parser:
3257
"""COLMAP parser."""
3358

@@ -166,27 +191,10 @@ def __init__(
166191
colmap_files = sorted(_get_rel_paths(colmap_image_dir))
167192
image_files = sorted(_get_rel_paths(image_dir))
168193
if factor > 1 and os.path.splitext(image_files[0])[1].lower() == ".jpg":
169-
print("Downscaling full resolution images instead of provided jpgs.")
170-
image_dir = image_dir + "_png"
171-
os.makedirs(image_dir, exist_ok=True)
172-
image_files = [
173-
os.path.splitext(image_file)[0] + ".png" for image_file in image_files
174-
]
175-
for colmap_file, image_file in zip(tqdm(colmap_files), image_files):
176-
resized_image_path = os.path.join(image_dir, image_file)
177-
if os.path.isfile(resized_image_path):
178-
continue
179-
full_image = imageio.imread(
180-
os.path.join(colmap_image_dir, colmap_file)
181-
)[..., :3]
182-
resized_size = (
183-
int(round(full_image.shape[1] / factor)),
184-
int(round(full_image.shape[0] / factor)),
185-
)
186-
resized_image = np.array(
187-
Image.fromarray(full_image).resize(resized_size, Image.BICUBIC)
188-
)
189-
imageio.imwrite(resized_image_path, resized_image)
194+
image_dir = _resize_image_folder(
195+
colmap_image_dir, image_dir + "_png", factor=factor
196+
)
197+
image_files = sorted(_get_rel_paths(image_dir))
190198
colmap_to_image = dict(zip(colmap_files, image_files))
191199
image_paths = [os.path.join(image_dir, colmap_to_image[f]) for f in image_names]
192200

0 commit comments

Comments
 (0)