Skip to content

Instantly share code, notes, and snippets.

@arsenyinfo
Created April 17, 2018 14:16
Show Gist options
  • Save arsenyinfo/74e42b41749cf29a7bbb69ed839bff1a to your computer and use it in GitHub Desktop.
Save arsenyinfo/74e42b41749cf29a7bbb69ed839bff1a 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
@arthurdeka
Copy link

Is this still working? i just keep getting errors

@alenwesker
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment