Skip to content

Instantly share code, notes, and snippets.

@forcewake
Forked from arsenyinfo/lut3d.py
Created May 16, 2019 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forcewake/4aacc7457c7d76fff1b64ba36bd47742 to your computer and use it in GitHub Desktop.
Save forcewake/4aacc7457c7d76fff1b64ba36bd47742 to your computer and use it in GitHub Desktop.
Apply 3D LUT to an image. It's not optimized yet, however works as an acceptable PoC
from functools import partial
import numpy as np
from tqdm import tqdm
LUT_SIZE = 33
def _convert(pixel, lut):
r, g, b = map(lambda x: round((x / 255) * LUT_SIZE - 1), pixel)
idx = r + g * LUT_SIZE + b * (LUT_SIZE ** 2)
result = lut[int(idx)]
r_, g_, b_ = map(lambda i: np.float(result[i]), range(3))
return np.array([r_, g_, b_])
def read_lut_file(path):
with open(path) as fd:
lines = [x.rstrip() for x in fd.readlines()]
lut = list(map(lambda x: x.split(' '), lines[-LUT_SIZE**3: ]))
return lut
def convert_with_lut(img, lut_path):
lut = read_lut_file(lut_path)
pixels = img.reshape(-1, 3)
convert = partial(_convert, lut=lut)
new_pixels = list(map(convert, tqdm(pixels)))
new_img = np.array(new_pixels).reshape(img.shape)
new_img = (new_img * 255).astype('uint8')
return new_img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment