Save image by pypng module.

    Args:
        path (str): output filename
        img (numpy.ndarray): Image array to save. Image shape is considered as (height, width, channel) by default.
        channel_first:
            This argument specifies the shape of img is whether (height, width, channel) or (channel, height, width).
            Default value is False, which means the img shape is (height, width, channel)
        as_uint16 (bool):
            If True, save image as uint16.
        auto_scale (bool) :
            Whether upscale pixel values or not.
            If you want to save float image, this argument must be True.
            In pypng backend, all below are supported.
                - float ([0, 1]) to uint8 ([0, 255])  (if img.dtype==float and upscale==True and as_uint16==False)
                - float to uint16 ([0, 65535]) (if img.dtype==float and upscale==True and as_uint16==True)
                - uint8 to uint16 are supported (if img.dtype==np.uint8 and upscale==True and as_uint16==True)

def imsave(path, img, channel_first=False, as_uint16=False, auto_scale=True):
    """
    Save image by pypng module.

    Args:
        path (str): output filename
        img (numpy.ndarray): Image array to save. Image shape is considered as (height, width, channel) by default.
        channel_first:
            This argument specifies the shape of img is whether (height, width, channel) or (channel, height, width).
            Default value is False, which means the img shape is (height, width, channel)
        as_uint16 (bool):
            If True, save image as uint16.
        auto_scale (bool) :
            Whether upscale pixel values or not.
            If you want to save float image, this argument must be True.
            In pypng backend, all below are supported.
                - float ([0, 1]) to uint8 ([0, 255])  (if img.dtype==float and upscale==True and as_uint16==False)
                - float to uint16 ([0, 65535]) (if img.dtype==float and upscale==True and as_uint16==True)
                - uint8 to uint16 are supported (if img.dtype==np.uint8 and upscale==True and as_uint16==True)
    """

    img = _imsave_before(img, channel_first, auto_scale)

    if auto_scale:
        img = upscale_pixel_intensity(img, as_uint16)

    img = check_type_and_cast_if_necessary(img, as_uint16)

    bitdepth = 8 if img.dtype == np.uint8 else 16
    grayscale = True if len(img.shape) == 2 or (
        len(img.shape) == 3 and img.shape[-1] == 1) else False

    writer = png.Writer(img.shape[1], img.shape[0],
                        greyscale=grayscale, bitdepth=bitdepth)

    writer.write(open(path, "wb"), img.reshape(img.shape[0], -1))