From bbc63538ffb992ea4dacdf27577423aac7f54d32 Mon Sep 17 00:00:00 2001
From: Christian Kento Rasmussen <christian.kento@gmail.com>
Date: Fri, 14 Jun 2024 10:37:04 +0200
Subject: [PATCH] added function to convert zarr to nifti

---
 qim3d/io/convert.py | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/qim3d/io/convert.py b/qim3d/io/convert.py
index 18b22fd6..5efee712 100644
--- a/qim3d/io/convert.py
+++ b/qim3d/io/convert.py
@@ -31,6 +31,8 @@ class Convert:
             match input_ext, output_ext:
                 case (".tif", ".zarr") | (".tiff", ".zarr"):
                     return self.convert_tif_to_zarr(input_path, output_path)
+                case (".nii", ".zarr"):
+                    return self.convert_nifti_to_zarr(input_path, output_path)
                 case _:
                     raise ValueError("Unsupported file format")
         # Load a directory
@@ -38,6 +40,8 @@ class Convert:
             match input_ext, output_ext:
                 case (".zarr", ".tif") | (".zarr", ".tiff"):
                     return self.convert_zarr_to_tif(input_path, output_path)
+                case (".zarr", ".nii"):
+                    return self.convert_zarr_to_tif(input_path, output_path)
                 case _:
                     raise ValueError("Unsupported file format")
         # Fail
@@ -126,6 +130,18 @@ class Convert:
 
         return z
 
+    def convert_zarr_to_nifti(self, zarr_path, nifti_path):
+        """Convert a zarr file to a nifti file
+
+        Args:
+            zarr_path (str): path to the zarr file
+            nifti_path (str): path to the nifti file
+
+        Returns:
+            None
+        """
+        z = zarr.open(zarr_path)
+        nib.save(nib.Nifti1Image(z, np.eye(4)), nifti_path)
 
 def convert(input_path: str, output_path: str, chunk_shape: tuple = (64, 64, 64)):
     """Convert a file to another format without loading the entire file into memory
-- 
GitLab