Exemplo n.º 1
0
def curve_to_lut(colorspace, gamma, outlutfile, out_type=None, out_format=None,
                 input_range=None, output_range=None, out_bit_depth=None,
                 out_cube_size=None, verbose=False, direction=Direction.ENCODE,
                 preset=None, overwrite_preset=False,
                 process_input_range=False):
    """Export a LUT from a colorspace gradation function

    Args:
        colorspace (str): input colorspace. Mutually exclusive with gamma.
        See list of colorspaces in utils.colorspaces

        gamma (float): input gamma. Mutually exclusive with colorspace.

        out_type (str): 1D, 2D or 3D

        out_format (str): '3dl', 'csp', 'cube', 'lut', 'spi', 'clcc', 'json'...

        outlutfile (str): path to output LUT

    Kwargs:

        input_range ([int/float, int/float]): input range.
        Ex: [0.0, 1.0] or [0, 4095]

        output_range ([int/float, int/float]): output range.
        Ex: [0.0, 1.0] or [0, 4095]

        out_bit_depth (int): output lut bit precision (1D only).
        Ex : 10, 16, 32.

        out_cube_size (int): output cube size (3D only). Ex : 17, 32.

        verbose (bool): print log if true

        direction (Direction): encode or decode

        preset (dict): lut generic and sampling informations

        process_input_range (bool): If true, input range will be computed from
        colorspace gradation functions. Colorspace only"

    """
    # get colorspace function
    if colorspace is None and gamma is None:
        raise AttributeError("A colorspace or a gamma should be specified")
    if colorspace is not None and gamma is not None:
        raise AttributeError("Choose between a colorspace or a gamma")
    elif gamma is not None:
        # gamma mode
        if direction == Direction.DECODE:
            gradation = lambda value: gamma_to_lin(value, gamma)
            title = "Gamma{0}_to_lin".format(gamma)
        else:
            gradation = lambda value: lin_to_gamma(value, gamma)
            title = "Lin_to_gamma{0}".format(gamma)
    else:
        # colorspace mode
        try:
            colorspace_obj = dict(list(COLORSPACES.items()) +
                                  list(PRIVATE_COLORSPACES.items()))[colorspace]
        except KeyError:
            raise CurveToLUTException(("Unsupported {0} "
                                       "Colorspace!").format(colorspace))
        if direction == Direction.DECODE:
            gradation = colorspace_obj.decode_gradation
            title = "{0}_to_lin".format(colorspace)
        else:
            gradation = colorspace_obj.encode_gradation
            title = "Lin_to_{0}".format(colorspace)
    # get preset and write function
    if preset:
        write_function = get_write_function(preset, overwrite_preset,
                                            out_type, out_format,
                                            input_range,
                                            output_range,
                                            out_bit_depth,
                                            out_cube_size,
                                            verbose)
    elif out_type is None or out_format is None:
        raise CurveToLUTException("Specify out_type/out_format or a preset.")
    else:
        preset, write_function = get_preset_and_write_function(out_type,
                                                               out_format,
                                                               input_range,
                                                               output_range,
                                                               out_bit_depth,
                                                               out_cube_size)
    if preset[presets.TYPE] == '3D':
        print_warning_message(("Gradations and gamma functions are 1D / 2D"
                               " transformations. Baking them in a 3D LUT "
                               "may not be efficient. Are you sure ?"))
    # process file output
    if os.path.isdir(outlutfile):
        filename = "{0}{1}".format(title,
                                   preset[presets.EXT])
        outlutfile = os.path.join(outlutfile, filename)
    else:
        try:
            check_extension(outlutfile, preset[presets.EXT])
            outlutfile = outlutfile
        except LUTException as error:
            raise CurveToLUTException(("Directory doesn't exist "
                                       "or {0}").format(error))
    preset[presets.TITLE] = title
    if process_input_range:
        if colorspace:
            preset[presets.IN_RANGE] = get_input_range(colorspace_obj,
                                                       direction,
                                                       8)
        else:
            raise CurveToLUTException(("--process-input-range must be used"
                                       " with --colorspace."))
    if verbose:
        print("{0} will be written in {1}.".format(title, outlutfile))
        print("Final setting:\n{0}".format(presets.string_preset(preset)))
    # write
    message = write_function(gradation, outlutfile, preset)
    if verbose:
        print_success_message(message)
Exemplo n.º 2
0
def curve_to_lut(colorspace, gamma, outlutfile, out_type=None, out_format=None,
                 input_range=None, output_range=None, out_bit_depth=None,
                 out_cube_size=None, verbose=False, direction=Direction.ENCODE,
                 preset=None, overwrite_preset=False,
                 process_input_range=False):
    """Export a LUT from a colorspace gradation function

    Args:
        colorspace (str): input colorspace. Mutually exclusive with gamma.
        See list of colorspaces in utils.colorspaces

        gamma (float): input gamma. Mutually exclusive with colorspace.

        out_type (str): 1D, 2D or 3D

        out_format (str): '3dl', 'csp', 'cube', 'lut', 'spi', 'clcc', 'json'...

        outlutfile (str): path to output LUT

    Kwargs:

        input_range ([int/float, int/float]): input range.
        Ex: [0.0, 1.0] or [0, 4095]

        output_range ([int/float, int/float]): output range.
        Ex: [0.0, 1.0] or [0, 4095]

        out_bit_depth (int): output lut bit precision (1D only).
        Ex : 10, 16, 32.

        out_cube_size (int): output cube size (3D only). Ex : 17, 32.

        verbose (bool): print log if true

        direction (Direction): encode or decode

        preset (dict): lut generic and sampling informations

        process_input_range (bool): If true, input range will be computed from
        colorspace gradation functions. Colorspace only"

    """
    # get colorspace function
    if colorspace is None and gamma is None:
        raise AttributeError("A colorspace or a gamma should be specified")
    if colorspace is not None and gamma is not None:
        raise AttributeError("Choose between a colorspace or a gamma")
    elif gamma is not None:
        # gamma mode
        if direction == Direction.DECODE:
            gradation = lambda value: gamma_to_lin(value, gamma)
            title = "Gamma{0}_to_lin".format(gamma)
        else:
            gradation = lambda value: lin_to_gamma(value, gamma)
            title = "Lin_to_gamma{0}".format(gamma)
    else:
        # colorspace mode
        try:
            colorspace_obj = dict(COLORSPACES.items() +
                                  PRIVATE_COLORSPACES.items())[colorspace]
        except KeyError:
            raise CurveToLUTException(("Unsupported {0} "
                                       "Colorspace!").format(colorspace))
        if direction == Direction.DECODE:
            gradation = colorspace_obj.decode_gradation
            title = "{0}_to_lin".format(colorspace)
        else:
            gradation = colorspace_obj.encode_gradation
            title = "Lin_to_{0}".format(colorspace)
    # get preset and write function
    if preset:
        write_function = get_write_function(preset, overwrite_preset,
                                            out_type, out_format,
                                            input_range,
                                            output_range,
                                            out_bit_depth,
                                            out_cube_size,
                                            verbose)
    elif out_type is None or out_format is None:
        raise CurveToLUTException("Specify out_type/out_format or a preset.")
    else:
        preset, write_function = get_preset_and_write_function(out_type,
                                                               out_format,
                                                               input_range,
                                                               output_range,
                                                               out_bit_depth,
                                                               out_cube_size)
    if preset[presets.TYPE] == '3D':
        print_warning_message(("Gradations and gamma functions are 1D / 2D"
                               " transformations. Baking them in a 3D LUT "
                               "may not be efficient. Are you sure ?"))
    # process file output
    if os.path.isdir(outlutfile):
        filename = "{0}{1}".format(title,
                                   preset[presets.EXT])
        outlutfile = os.path.join(outlutfile, filename)
    else:
        try:
            check_extension(outlutfile, preset[presets.EXT])
            outlutfile = outlutfile
        except LUTException as error:
            raise CurveToLUTException(("Directory doesn't exist "
                                       "or {0}").format(error))
    preset[presets.TITLE] = title
    if process_input_range:
        if colorspace:
            preset[presets.IN_RANGE] = get_input_range(colorspace_obj,
                                                       direction,
                                                       8)
        else:
            raise CurveToLUTException(("--process-input-range must be used"
                                       " with --colorspace."))
    if verbose:
        print "{0} will be written in {1}.".format(title, outlutfile)
        print "Final setting:\n{0}".format(presets.string_preset(preset))
    # write
    message = write_function(gradation, outlutfile, preset)
    if verbose:
        print_success_message(message)
Exemplo n.º 3
0
def lut_to_lut(inlutfiles,
               out_type=None,
               out_format=None,
               outlutfile=None,
               input_range=None,
               output_range=None,
               out_bit_depth=None,
               inverse=False,
               out_cube_size=None,
               verbose=False,
               smooth_size=None,
               preset=None,
               overwrite_preset=False):
    """ Concert a LUT in another LUT
    Arguments testing are delegated to LUT helpers

    Args:
        lutfiles (str or [str]): path to a LUT or list of LUT paths

        out_type (str): 1D, 2D or 3D

        out_format (str): '3dl', 'csp', 'cube', 'lut', 'spi', 'clcc', 'json'...

    Kwargs:
        outlutfile (str): path to output LUT

        input_range ([int/float, int/float]): input range.
        Ex: [0.0, 1.0] or [0, 4095]

        output_range ([int/float, int/float]): output range.
        Ex: [0.0, 1.0] or [0, 4095]

        out_bit_depth (int): output lut bit precision (1D only).
        Ex : 10, 16, 32.

        inverse (bool): inverse input LUT (1D only)

        out_cube_size (int): output cube size (3D only). Ex : 17, 32.

        verbose (bool): print log if true

        smooth_size (int): smooth exported LUT (1D only).
        Specify how many points are computed.
        A first subsampled curve is first processed and then resample with
        a smooth to fit input lutsize.
        So the smaller this value is, the smoother the curve will be.
        Ex: 10, 20,...

        preset (dict): lut generic and sampling informations

    """
    if preset:
        write_function = get_write_function(preset, overwrite_preset, out_type,
                                            out_format, input_range,
                                            output_range, out_bit_depth,
                                            out_cube_size, verbose)
    elif out_type is None or out_format is None:
        raise LutToLutException("Specify out_type/out_format or a preset.")
    else:
        preset, write_function = get_preset_and_write_function(
            out_type, out_format, input_range, output_range, out_bit_depth,
            out_cube_size)
    ext = preset[presets.EXT]
    if not isinstance(inlutfiles, (list, tuple)):
        inlutfiles = [inlutfiles]
    if not outlutfile:
        outlutfile = get_default_out_path(inlutfiles, ext)
    elif os.path.isdir(outlutfile):
        filename = os.path.splitext(ntpath.basename(inlutfiles[0]))[0] + ext
        outlutfile = os.path.join(outlutfile, filename)
    else:
        check_extension(outlutfile, ext)
    # smooth
    if smooth_size:
        preset[presets.SMOOTH] = smooth_size
    if verbose:
        print "{0} will be converted into {1}.".format(inlutfiles, outlutfile)
        print "Final setting:\n{0}".format(presets.string_preset(preset))
    processor = create_ocio_processor(inlutfiles,
                                      interpolation=INTERP_LINEAR,
                                      inverse=inverse)
    # change interpolation if 3D LUT
    if is_3d_lut(processor, inlutfiles[0]):
        processor = create_ocio_processor(inlutfiles,
                                          interpolation=INTERP_TETRAHEDRAL,
                                          inverse=inverse)
    # write LUT
    message = write_function(processor.applyRGB, outlutfile, preset)
    if verbose:
        print_success_message(message)
Exemplo n.º 4
0
def lut_to_lut(inlutfiles, out_type=None, out_format=None, outlutfile=None,
               input_range=None, output_range=None, out_bit_depth=None,
               inverse=False, out_cube_size=None, verbose=False,
               smooth_size=None, preset=None, overwrite_preset=False):
    """ Concert a LUT in another LUT
    Arguments testing are delegated to LUT helpers

    Args:
        lutfiles (str or [str]): path to a LUT or list of LUT paths

        out_type (str): 1D, 2D or 3D

        out_format (str): '3dl', 'csp', 'cube', 'lut', 'spi', 'clcc', 'json'...

    Kwargs:
        outlutfile (str): path to output LUT

        input_range ([int/float, int/float]): input range.
        Ex: [0.0, 1.0] or [0, 4095]

        output_range ([int/float, int/float]): output range.
        Ex: [0.0, 1.0] or [0, 4095]

        out_bit_depth (int): output lut bit precision (1D only).
        Ex : 10, 16, 32.

        inverse (bool): inverse input LUT (1D only)

        out_cube_size (int): output cube size (3D only). Ex : 17, 32.

        verbose (bool): print log if true

        smooth_size (int): smooth exported LUT (1D only).
        Specify how many points are computed.
        A first subsampled curve is first processed and then resample with
        a smooth to fit input lutsize.
        So the smaller this value is, the smoother the curve will be.
        Ex: 10, 20,...

        preset (dict): lut generic and sampling informations

    """
    if preset:
        write_function = get_write_function(preset, overwrite_preset,
                                            out_type, out_format,
                                            input_range,
                                            output_range,
                                            out_bit_depth,
                                            out_cube_size,
                                            verbose)
    elif out_type is None or out_format is None:
        raise LutToLutException("Specify out_type/out_format or a preset.")
    else:
        preset, write_function = get_preset_and_write_function(out_type,
                                                               out_format,
                                                               input_range,
                                                               output_range,
                                                               out_bit_depth,
                                                               out_cube_size)
    ext = preset[presets.EXT]
    if not isinstance(inlutfiles, (list, tuple)):
        inlutfiles = [inlutfiles]
    if not outlutfile:
        outlutfile = get_default_out_path(inlutfiles, ext)
    elif os.path.isdir(outlutfile):
        filename = os.path.splitext(ntpath.basename(inlutfiles[0]))[0] + ext
        outlutfile = os.path.join(outlutfile, filename)
    else:
        check_extension(outlutfile, ext)
    # smooth
    if smooth_size:
        preset[presets.SMOOTH] = smooth_size
    if verbose:
        print "{0} will be converted into {1}.".format(inlutfiles,
                                                       outlutfile)
        print "Final setting:\n{0}".format(presets.string_preset(preset))
    processor = create_ocio_processor(inlutfiles,
                                      interpolation=INTERP_LINEAR,
                                      inverse=inverse)
    # change interpolation if 3D LUT
    if is_3d_lut(processor, inlutfiles[0]):
        processor = create_ocio_processor(inlutfiles,
                                          interpolation=INTERP_TETRAHEDRAL,
                                          inverse=inverse)
    # write LUT
    message = write_function(processor.applyRGB, outlutfile, preset)
    if verbose:
        print_success_message(message)