Skip to content

Instantly share code, notes, and snippets.

@ychalier
Created July 20, 2019 12:06
Show Gist options
  • Save ychalier/aadc7ea180e088b2a568d1fd5b91c038 to your computer and use it in GitHub Desktop.
Save ychalier/aadc7ea180e088b2a568d1fd5b91c038 to your computer and use it in GitHub Desktop.
LUT converter into Darktable base curve
"""lut to darkatable basecurve converter
usage: python basecurve.py <NAME> <LUT>
"""
import struct
import sys
def hexify(blob):
def convert(byte):
hex_value = hex(byte)[2:]
return "0" * (2 - len(hex_value)) + hex_value
return "".join(map(convert, blob))
class Anchor:
def __init__(self, x, y):
self.x = x
self.y = y
def __lt__(self, other):
return self.x < other.x
def get_bytes(self):
return struct.pack("f", self.x) + struct.pack("f", self.y)
class BaseCurve(list):
MAX_NODES = 20
CURVE_TYPE = 2
def __init__(self):
super(BaseCurve, self).__init__()
def load_lut(self, path):
lut = list()
with open(path, "r") as file:
for i, line in enumerate(file.readlines()):
lut.append(Anchor(float(line.strip()) / 255, i / 255))
step = 256 // BaseCurve.MAX_NODES
for i in range(BaseCurve.MAX_NODES // 2):
self.append(lut[i * step])
self.append(lut[-1 - i * step])
self.sort()
def get_blob(self):
blob = b""
for anchor in self:
blob += anchor.get_bytes()
while len(blob) < 480:
blob += struct.pack("f", 0)
for i in [len(self), 0, 0, BaseCurve.CURVE_TYPE, 0, 0]:
blob += struct.pack("i", i)
return blob
def print_command(self, name, dbpath="~/.config/darktable/data.db"):
template = """echo \"INSERT INTO presets (name,description,operation,op_version,op_params,enabled,blendop_params,blendop_version,multi_priority,multi_name,model,maker,lens,iso_min,iso_max,exposure_min,exposure_max,aperture_min,aperture_max,focal_length_min,focal_length_max,writeprotect,autoapply,filter,def,format) VALUES('{name}','','basecurve',4,X'{blob}',1,X'00000000180000000000C842000000000000000000000000000000000000000000000000000000000000000000000000000000000000803F0000803F00000000000000000000803F0000803F00000000000000000000803F0000803F00000000000000000000803F0000803F00000000000000000000803F0000803F00000000000000000000803F0000803F00000000000000000000803F0000803F00000000000000000000803F0000803F00000000000000000000803F0000803F00000000000000000000803F0000803F00000000000000000000803F0000803F00000000000000000000803F0000803F00000000000000000000803F0000803F00000000000000000000803F0000803F00000000000000000000803F0000803F00000000000000000000803F0000803F',7,0,'','%','%','%',0.0,340282346638528859812000000000000000000,0.0,10000000.0,0.0,100000000.0,0.0,1000.0,0,0,0,0,2);\" | sqlite3 {dbpath}\n"""
print(template.format(
name=name,
blob=hexify(self.get_blob()).upper(),
dbpath=dbpath
))
if __name__ == "__main__":
if len(sys.argv) != 3:
print(__doc__)
else:
script, name, lut = sys.argv
curve = BaseCurve()
curve.load_lut(lut)
curve.print_command(name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment