| import numpy as np |
| import PIL.Image |
| from PIL.PngImagePlugin import PngInfo |
|
|
|
|
| def read_depth_from_png(file_path): |
| depth = PIL.Image.open(file_path) |
| max_depth = float(depth.info["max_value"]) |
| min_depth = float(depth.info["min_value"]) |
| depth = np.array(depth).astype(np.float32) |
|
|
| |
| depth = (depth / (2 ** 16 - 1)) * (max_depth - min_depth) + min_depth |
|
|
| |
| depth[depth == -1.0] = np.inf |
| return depth |
|
|
|
|
| def write_depth_to_png(outpath_file, depth_pred): |
| |
| depth_pred[np.isinf(depth_pred)] = -1.0 |
|
|
| min_depth = np.min(depth_pred) |
| max_depth = np.max(depth_pred) |
|
|
| |
| depth_pred_normalized = (depth_pred - min_depth) / (max_depth - min_depth) |
|
|
| |
| depth_pred_scaled = np.round(depth_pred_normalized * (2 ** 16 - 1)).astype(np.uint16) |
|
|
| |
| depth_pred_img = PIL.Image.fromarray(depth_pred_scaled) |
| metadata = PngInfo() |
| metadata.add_text("max_value", str(max_depth)) |
| metadata.add_text("min_value", str(min_depth)) |
| depth_pred_img.save(outpath_file, |
| format="PNG", mode="I", pnginfo=metadata) |
|
|
|
|