Skip to content
Snippets Groups Projects
Select Git revision
  • 11da9c422ab91ac6088a1b94a99e89b3174f1872
  • main default protected
  • 3D_UNet
  • notebooksv1
  • scaleZYX_mean
  • notebooks
  • convert_tiff_folders
  • test
  • notebook_update
  • threshold-exploration
  • optimize_scaleZYXdask
  • layered_surface_segmentation
  • conv_zarr_tiff_folders
  • 3d_watershed
  • tr_val_te_splits
  • save_files_function
  • memmap_txrm
  • v0.4.1
  • v0.4.0
  • v0.3.9
  • v0.3.3
  • v0.3.2
  • v0.3.1
  • v0.3.0
  • v0.2.0
25 results

index.md

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    convert.py 1.48 KiB
    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)