X-Git-Url: http://users.mur.at/ms/git/gitweb/?a=blobdiff_plain;f=aces_1.0.0%2Fpython%2Faces_ocio%2Fgenerate_lut.py;h=4ed78fe92fc27abdf5ea3bb0978edd17afb3110b;hb=1c4efc5c67e2313dfadf4a0786baaabd8d6489a6;hp=78bc9f6a6dace4e5c9852a8c83718d42537e66ca;hpb=2158c15470d514412974585e84210cbc2c909011;p=OpenColorIO-Configs.git diff --git a/aces_1.0.0/python/aces_ocio/generate_lut.py b/aces_1.0.0/python/aces_ocio/generate_lut.py old mode 100644 new mode 100755 index 78bc9f6..4ed78fe --- a/aces_1.0.0/python/aces_ocio/generate_lut.py +++ b/aces_1.0.0/python/aces_ocio/generate_lut.py @@ -52,13 +52,8 @@ def generate_1d_LUT_image(ramp_1d_path, Return value description. """ - # print('Generate 1d LUT image - %s' % ramp1dPath) - - # open image - format = os.path.splitext(ramp_1d_path)[1] ramp = oiio.ImageOutput.create(ramp_1d_path) - # set image specs spec = oiio.ImageSpec() spec.set_format(oiio.FLOAT) # spec.format.basetype = oiio.FLOAT @@ -99,19 +94,18 @@ def write_SPI_1d(filename, from_min, from_max, data, entries, channels): Return value description. """ - f = file(filename, 'w') - f.write('Version 1\n') - f.write('From %f %f\n' % (from_min, from_max)) - f.write('Length %d\n' % entries) - f.write('Components %d\n' % (min(3, channels))) - f.write('{\n') - for i in range(0, entries): - entry = '' - for j in range(0, min(3, channels)): - entry = '%s %s' % (entry, data[i * channels + j]) - f.write(' %s\n' % entry) - f.write('}\n') - f.close() + with open(filename, 'w') as fp: + fp.write('Version 1\n') + fp.write('From %f %f\n' % (from_min, from_max)) + fp.write('Length %d\n' % entries) + fp.write('Components %d\n' % (min(3, channels))) + fp.write('{\n') + for i in range(0, entries): + entry = '' + for j in range(0, min(3, channels)): + entry = '%s %s' % (entry, data[i * channels + j]) + fp.write(' %s\n' % entry) + fp.write('}\n') def generate_1d_LUT_from_image(ramp_1d_path, @@ -133,21 +127,16 @@ def generate_1d_LUT_from_image(ramp_1d_path, """ if output_path is None: - output_path = ramp_1d_path + '.spi1d' + output_path = '%s.%s' % (ramp_1d_path, 'spi1d') - # open image ramp = oiio.ImageInput.open(ramp_1d_path) - # get image specs spec = ramp.spec() - type = spec.format.basetype width = spec.width - height = spec.height channels = spec.nchannels - # get data - # Force data to be read as float. The Python API doesn't handle - # half-floats well yet. + # Forcibly read data as float, the Python API doesn't handle half-float + # well yet. type = oiio.FLOAT data = ramp.read_image(type) @@ -198,7 +187,7 @@ def generate_3d_LUT_from_image(ramp_3d_path, output_path=None, resolution=32): """ if output_path is None: - output_path = ramp_3d_path + '.spi3d' + output_path = '%s.%s' % (ramp_3d_path, 'spi1d') args = ['--extract', '--cubesize', @@ -217,11 +206,11 @@ def generate_3d_LUT_from_image(ramp_3d_path, output_path=None, resolution=32): def apply_CTL_to_image(input_image, output_image, - ctl_paths=[], + ctl_paths=None, input_scale=1.0, output_scale=1.0, - global_params={}, - aces_CTL_directory=None): + global_params=None, + aces_ctl_directory=None): """ Object description. @@ -236,20 +225,24 @@ def apply_CTL_to_image(input_image, Return value description. """ + if ctl_paths is None: + ctl_paths = [] + if global_params is None: + global_params = {} + if len(ctl_paths) > 0: ctlenv = os.environ - if aces_CTL_directory != None: - if os.path.split(aces_CTL_directory)[1] != 'utilities': - ctl_module_path = '%s/utilities' % aces_CTL_directory + if aces_ctl_directory is not None: + if os.path.split(aces_ctl_directory)[1] != 'utilities': + ctl_module_path = os.path.join(aces_ctl_directory, 'utilities') else: - ctl_module_path = aces_CTL_directory + ctl_module_path = aces_ctl_directory ctlenv['CTL_MODULE_PATH'] = ctl_module_path args = [] for ctl in ctl_paths: args += ['-ctl', ctl] args += ['-force'] - # args += ['-verbose'] args += ['-input_scale', str(input_scale)] args += ['-output_scale', str(output_scale)] args += ['-global_param1', 'aIn', '1.0'] @@ -258,8 +251,6 @@ def apply_CTL_to_image(input_image, args += [input_image] args += [output_image] - # print('args : %s' % args) - ctlp = Process(description='a ctlrender process', cmd='ctlrender', args=args, env=ctlenv) @@ -299,9 +290,9 @@ def generate_1d_LUT_from_CTL(lut_path, identity_LUT_bit_depth='half', input_scale=1.0, output_scale=1.0, - global_params={}, + global_params=None, cleanup=True, - aces_CTL_directory=None, + aces_ctl_directory=None, min_value=0.0, max_value=1.0): """ @@ -318,33 +309,33 @@ def generate_1d_LUT_from_CTL(lut_path, Return value description. """ - # print(lutPath) - # print(ctlPaths) + if global_params is None: + global_params = {} lut_path_base = os.path.splitext(lut_path)[0] - identity_LUT_image_float = lut_path_base + '.float.tiff' + identity_LUT_image_float = '%s.%s.%s' % (lut_path_base, 'float', 'tiff') generate_1d_LUT_image(identity_LUT_image_float, lut_resolution, min_value, max_value) if identity_LUT_bit_depth != 'half': - identity_LUT_image = lut_path_base + '.uint16.tiff' + identity_LUT_image = '%s.%s.%s' % (lut_path_base, 'uint16', 'tiff') convert_bit_depth(identity_LUT_image_float, identity_LUT_image, identity_LUT_bit_depth) else: identity_LUT_image = identity_LUT_image_float - transformed_LUT_image = lut_path_base + '.transformed.exr' + transformed_LUT_image = '%s.%s.%s' % (lut_path_base, 'transformed', 'exr') apply_CTL_to_image(identity_LUT_image, transformed_LUT_image, ctl_paths, input_scale, output_scale, global_params, - aces_CTL_directory) + aces_ctl_directory) generate_1d_LUT_from_image(transformed_LUT_image, lut_path, @@ -375,17 +366,13 @@ def correct_LUT_image(transformed_LUT_image, Return value description. """ - # open image transformed = oiio.ImageInput.open(transformed_LUT_image) - # get image specs transformed_spec = transformed.spec() - type = transformed_spec.format.basetype width = transformed_spec.width height = transformed_spec.height channels = transformed_spec.nchannels - # rotate or not if width != lut_resolution * lut_resolution or height != lut_resolution: print(('Correcting image as resolution is off. ' 'Found %d x %d. Expected %d x %d') % ( @@ -395,20 +382,13 @@ def correct_LUT_image(transformed_LUT_image, lut_resolution)) print('Generating %s' % corrected_LUT_image) - # - # We're going to generate a new correct image - # - - # Get the source data - # Force data to be read as float. The Python API doesn't handle - # half-floats well yet. + # Forcibly read data as float, the Python API doesn't handle half-float + # well yet. type = oiio.FLOAT source_data = transformed.read_image(type) - format = os.path.splitext(corrected_LUT_image)[1] correct = oiio.ImageOutput.create(corrected_LUT_image) - # set image specs correct_spec = oiio.ImageSpec() correct_spec.set_format(oiio.FLOAT) correct_spec.width = height @@ -424,7 +404,6 @@ def correct_LUT_image(transformed_LUT_image, for j in range(0, correct_spec.height): for i in range(0, correct_spec.width): for c in range(0, correct_spec.nchannels): - # print(i, j, c) dest_data[(correct_spec.nchannels * correct_spec.width * j + correct_spec.nchannels * i + c)] = ( @@ -449,9 +428,9 @@ def generate_3d_LUT_from_CTL(lut_path, identity_LUT_bit_depth='half', input_scale=1.0, output_scale=1.0, - global_params={}, + global_params=None, cleanup=True, - aces_CTL_directory=None): + aces_ctl_directory=None): """ Object description. @@ -466,35 +445,34 @@ def generate_3d_LUT_from_CTL(lut_path, Return value description. """ - # print(lutPath) - # print(ctlPaths) + if global_params is None: + global_params = {} lut_path_base = os.path.splitext(lut_path)[0] - identity_LUT_image_float = lut_path_base + '.float.tiff' + identity_LUT_image_float = '%s.%s.%s' % (lut_path_base, 'float', 'tiff') generate_3d_LUT_image(identity_LUT_image_float, lut_resolution) if identity_LUT_bit_depth != 'half': - identity_LUT_image = (lut_path_base + - '.' + - identity_LUT_bit_depth + - '.tiff') + identity_LUT_image = '%s.%s.%s' % (lut_path_base, + identity_LUT_bit_depth, + 'tiff') convert_bit_depth(identity_LUT_image_float, identity_LUT_image, identity_LUT_bit_depth) else: identity_LUT_image = identity_LUT_image_float - transformed_LUT_image = lut_path_base + '.transformed.exr' + transformed_LUT_image = '%s.%s.%s' % (lut_path_base, 'transformed', 'exr') apply_CTL_to_image(identity_LUT_image, transformed_LUT_image, ctl_paths, input_scale, output_scale, global_params, - aces_CTL_directory) + aces_ctl_directory) - corrected_LUT_image = lut_path_base + '.correct.exr' + corrected_LUT_image = '%s.%s.%s' % (lut_path_base, 'correct', 'exr') corrected_LUT_image = correct_LUT_image(transformed_LUT_image, corrected_LUT_image, lut_resolution) @@ -508,7 +486,6 @@ def generate_3d_LUT_from_CTL(lut_path, os.remove(transformed_LUT_image) if corrected_LUT_image != transformed_LUT_image: os.remove(corrected_LUT_image) - # os.remove(correctedLUTImage) def main(): @@ -536,8 +513,8 @@ def main(): p.add_option('--lut', '-l', type='string', default='') p.add_option('--ctl', '-c', type='string', action='append') - p.add_option('--lut_resolution_1d', '', type='int', default=1024) - p.add_option('--lut_resolution_3d', '', type='int', default=33) + p.add_option('--lutResolution1d', '', type='int', default=1024) + p.add_option('--lutResolution3d', '', type='int', default=33) p.add_option('--ctlReleasePath', '-r', type='string', default='') p.add_option('--bitDepth', '-b', type='string', default='float') p.add_option('--keepTempImages', '', action='store_true') @@ -553,9 +530,6 @@ def main(): options, arguments = p.parse_args() - # - # Get options - # lut = options.lut ctls = options.ctl lut_resolution_1d = options.lut_resolution_1d @@ -567,11 +541,11 @@ def main(): ctl_release_path = options.ctlReleasePath generate_1d = options.generate1d is True generate_3d = options.generate3d is True - bitdepth = options.bitDepth + bit_depth = options.bitDepth cleanup = not options.keepTempImages params = {} - if options.ctlRenderParam != None: + if options.ctlRenderParam is not None: for param in options.ctlRenderParam: params[param[0]] = float(param[1]) @@ -582,11 +556,6 @@ def main(): args_start = len(sys.argv) + 1 args = [] - # print('command line : \n%s\n' % ' '.join(sys.argv)) - - # - # Generate LUTs - # if generate_1d: print('1D LUT generation options') else: @@ -602,14 +571,14 @@ def main(): print('output scale : %s' % output_scale) print('ctl render params : %s' % params) print('ctl release path : %s' % ctl_release_path) - print('bit depth of input : %s' % bitdepth) + print('bit depth of input : %s' % bit_depth) print('cleanup temp images : %s' % cleanup) if generate_1d: generate_1d_LUT_from_CTL(lut, ctls, lut_resolution_1d, - bitdepth, + bit_depth, input_scale, output_scale, params, @@ -622,7 +591,7 @@ def main(): generate_3d_LUT_from_CTL(lut, ctls, lut_resolution_3d, - bitdepth, + bit_depth, input_scale, output_scale, params,