@@ -420,28 +420,36 @@ def write_im(path, im, axes, pixel_size=None):
420420 # convert the image to expects (Z)CYX axis order
421421 if im .ndim == 4 :
422422 assert set (axes ) == {"b" , "x" , "y" , "c" }, f"{ axes } "
423- axes_ij = "cyxb"
423+ resolution_axes_ij = "cyxb"
424424 else :
425425 assert set (axes ) == {"b" , "x" , "y" , "z" , "c" }, f"{ axes } "
426- axes_ij = "zcyxb"
427-
428- axis_permutation = tuple (axes .index (ax ) for ax in axes_ij )
426+ resolution_axes_ij = "bzcyx"
427+
428+ def addMissingAxes (im_axes ):
429+ needed_axes = ["b" , "c" , "x" , "y" , "z" , "s" ]
430+ for ax in needed_axes :
431+ if not ax in im_axes :
432+ im_axes += ax
433+ return im_axes
434+
435+ axes_ij = "bzcyxs"
436+ # Expand the image to ImageJ dimensions
437+ im = np .expand_dims (im , axis = tuple (range (len (axes ),len (axes_ij ))))
438+
439+
440+ axis_permutation = tuple (addMissingAxes (axes ).index (ax ) for ax in axes_ij )
429441 im = im .transpose (axis_permutation )
430- # expand to TZCYXS
431- if len (axes_ij ) == 4 : # add singleton t and z axis
432- im = im [None , None ]
433- else : # add singeton z axis
434- im = im [None ]
442+
435443
436444 if pixel_size is None :
437445 resolution = None
438446 else :
439- spatial_axes = list (set (axes_ij ) - set ("bc" ))
440- resolution = tuple (1.0 / pixel_size [ax ] for ax in axes_ij if ax in spatial_axes )
447+ spatial_axes = list (set (resolution_axes_ij ) - set ("bc" ))
448+ resolution = tuple (1.0 / pixel_size [ax ] for ax in resolution_axes_ij if ax in spatial_axes )
441449 # does not work for double
442450 if np .dtype (im .dtype ) == np .dtype ("float64" ):
443451 im = im .astype ("float32" )
444- tifffile .imsave (path , im , imagej = True , resolution = resolution )
452+ tifffile .imwrite (path , im , imagej = True , resolution = resolution )
445453
446454 sample_in_paths = []
447455 for i , (in_path , axes ) in enumerate (zip (input_paths , input_axes )):
0 commit comments