Skip to content

Instantly share code, notes, and snippets.

@UnaiM
Last active August 22, 2023 21:09
Show Gist options
  • Save UnaiM/3780ed97a3ef8d64541ae6595d5ebbb4 to your computer and use it in GitHub Desktop.
Save UnaiM/3780ed97a3ef8d64541ae6595d5ebbb4 to your computer and use it in GitHub Desktop.
Given a LUT file, plot its luma EOTF to a tiny image
#! /usr/bin/env python
import argparse
import OpenImageIO as oiio
LUMA = (0.2126, 0.7152, 0.0722)
def plot_lut(lutpath, outpath):
size = None
mult = None
out = None
count = 0
with open(lutpath) as lut:
for line in lut:
line = line.strip()
if not line:
continue
if size:
if count % mult == 0:
v = sum(c * l for c, l in zip((float(s) for s in line.split(' ')), LUMA))
out.setpixel(count / mult, size - int(round(v * (size - 1))), (1.0, 1.0, 1.0))
count += 1
continue
if not size and line.startswith('LUT_3D_SIZE'):
size = int(line[11:].lstrip())
mult = size ** 3 / size + size + 1
out = oiio.ImageBuf(oiio.ImageSpec(size, size, 3, oiio.FLOAT))
oiio.ImageBufAlgo.fill(out, (0, 0, 0))
out.write(outpath)
if __name__ == '__main__':
parser = argparse.ArgumentParser(epilog='The paths can have any extension understood by OpenImageIO')
parser.add_argument('lutpath', metavar='/path/to/lut.cube')
parser.add_argument('outpath', metavar='/path/to/output.png')
args = parser.parse_args()
plot_lut(args.lutpath, args.outpath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment