-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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