utils2p/main.py
def save_img(path,
             img,
             imagej=True,
             color=False,
             full_dynamic_range=True,
             metadata=None):
    """
    Saves an image that is given as a numpy array to file.

    Parameters
    ----------
    path : string
        Path where the file is saved.
    img : numpy array
        Image or stack. For stacks, the first dimension is the stack index.
        For color images, the last dimension are the RGB channels.
    imagej : boolean
        Save imagej compatible stacks and hyperstacks.
    color : boolean, default = False
        Determines if image is RGB or gray scale.
        Will be converted to uint8.
    full_dynamic_range : boolean, default = True
        When an image is converted to uint8 for saving a color image the
        max value of the output image is the max of uint8,
        i.e. the image uses the full dynamic range available.
    """
    if img.dtype == np.bool:
        img = img.astype(np.uint8) * 255
    path = os.path.expanduser(os.path.expandvars(path))
    if color:
        if img.dtype != np.uint8:
            old_max = np.max(img, axis=tuple(range(img.ndim - 1)))
            if not full_dynamic_range:
                if np.issubdtype(img.dtype, np.integer):
                    old_max = np.iinfo(img.dtype).max * np.ones(3)
                elif np.issubdtype(img.dtype, np.floating):
                    old_max = np.finfo(img.dtype).max * np.ones(3)
                else:
                    raise ValueError(
                        f"img must be integer or float type not {img.dtype}")
            new_max = np.iinfo(np.uint8).max
            img = img / old_max * new_max
            img = img.astype(np.uint8)
        if imagej and img.ndim == 4:
            img = np.expand_dims(img, axis=1)
        if imagej and img.ndim == 3:
            img = np.expand_dims(img, axis=0)
            img = np.expand_dims(img, axis=1)
    else:
        if imagej and img.ndim == 4:
            img = np.expand_dims(img, axis=2)
            img = np.expand_dims(img, axis=5)
        if imagej and img.ndim == 3:
            img = np.expand_dims(img, axis=1)
            img = np.expand_dims(img, axis=4)
    if img.dtype == np.float64:
        img = img.astype(np.float32)
    if metadata is None:
        tifffile.imsave(path, img, imagej=imagej)
    else:
        # TODO add meta data like metadata={'xresolution':'4.25','yresolution':'0.0976','PixelAspectRatio':'43.57'}
        # tifffile.imsave(path, img, imagej=imagej, metadata={})
        raise NotImplementedError("Saving of metadata is not yet implemented")

    Saves an image that is given as a numpy array to file.

    Parameters
    ----------
    path : string
        Path where the file is saved.
    img : numpy array
        Image or stack. For stacks, the first dimension is the stack index.
        For color images, the last dimension are the RGB channels.
    imagej : boolean
        Save imagej compatible stacks and hyperstacks.
    color : boolean, default = False
        Determines if image is RGB or gray scale.
        Will be converted to uint8.
    full_dynamic_range : boolean, default = True
        When an image is converted to uint8 for saving a color image the
        max value of the output image is the max of uint8,
        i.e. the image uses the full dynamic range available.
    
Saves an image that is given as a numpy array to file.
Parameters

path : string
Path where the file is saved.
img : numpy array
Image or stack. For stacks, the first dimension is the stack index.
For color images, the last dimension are the RGB channels.
imagej : boolean
Save imagej compatible stacks and hyperstacks.
color : boolean, default = False
Determines if image is RGB or gray scale.
Will be converted to uint8.
full_dynamic_range : boolean, default = True
When an image is converted to uint8 for saving a color image the
max value of the output image is the max of uint8,
i.e. the image uses the full dynamic range available.
Saves an image that is given as a numpy array to file . Parameters path : string Path where the file is saved . img : numpy array Image or stack . For stacks the first dimension is the stack index . For color images the last dimension are the RGB channels . imagej : boolean Save imagej compatible stacks and hyperstacks . color : boolean default = False Determines if image is RGB or gray scale . Will be converted to uint8 . full_dynamic_range : boolean default = True When an image is converted to uint8 for saving a color image the max value of the output image is the max of uint8 i . e . the image uses the full dynamic range available .
def save_img(path,
             img,
             imagej=True,
             color=False,
             full_dynamic_range=True,
             metadata=None):
    if img.dtype == np.bool:
        img = img.astype(np.uint8) * 255
    path = os.path.expanduser(os.path.expandvars(path))
    if color:
        if img.dtype != np.uint8:
            old_max = np.max(img, axis=tuple(range(img.ndim - 1)))
            if not full_dynamic_range:
                if np.issubdtype(img.dtype, np.integer):
                    old_max = np.iinfo(img.dtype).max * np.ones(3)
                elif np.issubdtype(img.dtype, np.floating):
                    old_max = np.finfo(img.dtype).max * np.ones(3)
                else:
                    raise ValueError(
                        f"img must be integer or float type not {img.dtype}")
            new_max = np.iinfo(np.uint8).max
            img = img / old_max * new_max
            img = img.astype(np.uint8)
        if imagej and img.ndim == 4:
            img = np.expand_dims(img, axis=1)
        if imagej and img.ndim == 3:
            img = np.expand_dims(img, axis=0)
            img = np.expand_dims(img, axis=1)
    else:
        if imagej and img.ndim == 4:
            img = np.expand_dims(img, axis=2)
            img = np.expand_dims(img, axis=5)
        if imagej and img.ndim == 3:
            img = np.expand_dims(img, axis=1)
            img = np.expand_dims(img, axis=4)
    if img.dtype == np.float64:
        img = img.astype(np.float32)
    if metadata is None:
        tifffile.imsave(path, img, imagej=imagej)
    else:
        raise NotImplementedError("Saving of metadata is not yet implemented")
def save_img ( path , img , imagej = True , color = False , full_dynamic_range = True , metadata = None ) : if img . dtype == np . bool : img = img . astype ( np . uint8 ) * 255 path = os . path . expanduser ( os . path . expandvars ( path ) ) if color : if img . dtype != np . uint8 : old_max = np . max ( img , axis = tuple ( range ( img . ndim - 1 ) ) ) if not full_dynamic_range : if np . issubdtype ( img . dtype , np . integer ) : old_max = np . iinfo ( img . dtype ) . max * np . ones ( 3 ) elif np . issubdtype ( img . dtype , np . floating ) : old_max = np . finfo ( img . dtype ) . max * np . ones ( 3 ) else : raise ValueError ( f"img must be integer or float type not {img.dtype}" ) new_max = np . iinfo ( np . uint8 ) . max img = img / old_max * new_max img = img . astype ( np . uint8 ) if imagej and img . ndim == 4 : img = np . expand_dims ( img , axis = 1 ) if imagej and img . ndim == 3 : img = np . expand_dims ( img , axis = 0 ) img = np . expand_dims ( img , axis = 1 ) else : if imagej and img . ndim == 4 : img = np . expand_dims ( img , axis = 2 ) img = np . expand_dims ( img , axis = 5 ) if imagej and img . ndim == 3 : img = np . expand_dims ( img , axis = 1 ) img = np . expand_dims ( img , axis = 4 ) if img . dtype == np . float64 : img = img . astype ( np . float32 ) if metadata is None : tifffile . imsave ( path , img , imagej = imagej ) else : raise NotImplementedError ( "Saving of metadata is not yet implemented" )
Saves an image that is given as a numpy array to file.
Saves an image that is given as a numpy array to file .
"""
    Saves an image that is given as a numpy array to file.

    Parameters
    ----------
    path : string
        Path where the file is saved.
    img : numpy array
        Image or stack. For stacks, the first dimension is the stack index.
        For color images, the last dimension are the RGB channels.
    imagej : boolean
        Save imagej compatible stacks and hyperstacks.
    color : boolean, default = False
        Determines if image is RGB or gray scale.
        Will be converted to uint8.
    full_dynamic_range : boolean, default = True
        When an image is converted to uint8 for saving a color image the
        max value of the output image is the max of uint8,
        i.e. the image uses the full dynamic range available.
    """ # TODO add meta data like metadata={'xresolution':'4.25','yresolution':'0.0976','PixelAspectRatio':'43.57'} # tifffile.imsave(path, img, imagej=imagej, metadata={})
path
img
imagej
color
full_dynamic_range
metadata