in utils/image_io.py [0:0]
def save_raw_float32_image(file_name, image):
with open(file_name, "wb") as f:
CV_CN_MAX = 512
CV_CN_SHIFT = 3
CV_32F = 5
dims = image.shape
h = 0
w = 0
d = 1
if len(dims) == 2:
h, w = image.shape
float32_image = np.transpose(image).astype(np.float32)
else:
h, w, d = image.shape
float32_image = np.transpose(image, [2, 1, 0]).astype("float32")
cv_type = CV_32F + ((d - 1) << CV_CN_SHIFT)
pixel_size = d * 4
if d > CV_CN_MAX:
raise Exception("Cannot save image with more than 512 channels")
f.write(struct.pack("i", h))
f.write(struct.pack("i", w))
f.write(struct.pack("i", cv_type))
f.write(struct.pack("Q", pixel_size)) # Write size_t ~ uint64_t
# Set buffer size to 16 MiB to hide the Python loop overhead.
buffersize = max(16 * 1024 ** 2 // image.itemsize, 1)
for chunk in np.nditer(
float32_image,
flags=["external_loop", "buffered", "zerosize_ok"],
buffersize=buffersize,
order="F",
):
f.write(chunk.tobytes("C"))