def write_image()

in src/spg/utils/imageUtils.py [0:0]


def write_image(image, filename, bit_depth, channel_mapping=None):
    """ Writes the given image buffer to the file name provided

    :param image: The ImageBuf we want to write to disk
    :param filename: the full file path with extension we want to write the file
    :param bit_depth: The bit depth we want to write the image out as
    :param channel_mapping: the order of the channels we want to write out, "RGB", "BGR", "RBG" etc we work in rgb by
        default and swap if specified. Ie pattern generators force the swap, the raster stitching keeps what its given
    :return: The filepath to the image we just wrote out
    """
    # The images have been created within a floating point buffer, but some of the patterns have been designed to
    # calculate values using a lower bit depth for a given imaging chain. However if we are writing out exr we
    # always want to keep the float point values
    if filename.endswith(".exr"):
        bit_depth = "half"

    if filename.endswith(".tif") or filename.endswith(".tiff"):
        bit_depth = 16

    if channel_mapping:
        mapping_order = [char for char in channel_mapping]
        image = ImageBufAlgo.channels(
            image, tuple(mapping_order)
        )

    if not image.has_error:
        # We ensure we are writing out none compressed images
        image.specmod().attribute(
            constants.OCIO_COMPRESSION_ATTRIBUTE, constants.OCIO_COMPRESSION_NONE
        )

        # We explicitly set the bits per sample for 10 and 12 bit images
        oiio_bit_depth = get_oiio_bit_depth(bit_depth)
        if bit_depth == 10 or bit_depth == 12:
            image.specmod().attribute(constants.OIIO_BITS_PER_SAMPLE, bit_depth)

        image.write(filename, oiio_bit_depth)
        return filename

    if image.has_error:
        raise IOError("Error writing", filename, ":", image.geterror())