From 78d314bb7b3f4fd703d22b2bafdd1e32c5cfe4ff Mon Sep 17 00:00:00 2001 From: Christian Kento Rasmussen <christian.kento@gmail.com> Date: Thu, 30 May 2024 10:11:30 +0200 Subject: [PATCH] Began work on convert tool --- qim3d/io/convert.py | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 qim3d/io/convert.py diff --git a/qim3d/io/convert.py b/qim3d/io/convert.py new file mode 100644 index 00000000..e08629d8 --- /dev/null +++ b/qim3d/io/convert.py @@ -0,0 +1,51 @@ +import numpy as np +import tifffile as tiff +import zarr + + +def convert_tif_to_zarr(tif_path, zarr_path, chunks=(64, 64, 64)): + """ Convert a tiff file to a zarr file + + Args: + tif_path (str): path to the tiff file + zarr_path (str): path to the zarr file + chunks (tuple, optional): chunk size for the zarr file. Defaults to (64, 64, 64). + + Returns: + zarr.core.Array: zarr array containing the data from the tiff file + """ + vol = tiff.memmap(tif_path) + z = zarr.open(zarr_path, mode='w', shape=vol.shape, chunks=chunks, dtype=vol.dtype) + z[:] = vol[:] + + return z + +def convert_npy_to_zarr(npy_path, zarr_path, shape, dtype=np.float32, chunks=(64, 64, 64)): + """ Convert a numpy file to a zarr file + + Args: + npy_path (str): path to the numpy file + zarr_path (str): path to the zarr file + chunks (tuple, optional): chunk size for the zarr file. Defaults to (64, 64, 64). + + Returns: + zarr.core.Array: zarr array containing the data from the numpy file + """ + vol = np.memmap(npy_path, dtype=dtype, mode='r', shape=shape) + z = zarr.open(zarr_path, mode='w', shape=vol.shape, chunks=chunks, dtype=vol.dtype) + z[:] = vol[:] + + return z + +def convert_zarr_to_tif(zarr_path, tif_path): + """ Convert a zarr file to a tiff file + + Args: + zarr_path (str): path to the zarr file + tif_path (str): path to the tiff file + + returns: + None + """ + z = zarr.open(zarr_path) + tiff.imwrite(tif_path, z) \ No newline at end of file -- GitLab