Hald Image to 3D CLUT (.cube)

先安裝 Imagemagick 套件

使用 convert 產生 Hald Color LUT (Look Up Table )

convert hald:5 hald.png

將會產生一張 5x5x5 的色彩速查圖 (Hald Image)

我們可將這張圖用任何攝影軟體 (VSCO, Lightroom, Photoshop…等) 套用色彩效果後

用以下這段 python 程式轉成 .cube

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python

from __future__ import print_function, division

import math
import sys
from optparse import OptionParser

from PIL import Image


def main():

opt_parser = OptionParser(usage='%prog [options] input.png output.cube')
opts, args = opt_parser.parse_args()

if len(args) != 2:
opt_parser.print_usage()
exit(1)

in_ = Image.open(args[0])
w, h = in_.size
if w != h:
print('HALD input is not square.', file=sys.stderr)
exit(2)
steps = int(round(math.pow(w, 1/3)))
if steps ** 3 != w:
print('HALD input size is invalid: %d is not a cube.' % w, file=sys.stderr)

print('%d steps -> %d values' % (steps, steps**6), file=sys.stderr)
# Assume that we are going from 8 bits to 10.

out = open(args[1], 'w')
# out.write('#Created by: Adobe Photoshop CS6\n')
# out.write('#Copyright: Copyright 2012 Adobe Systems Inc.\n')
out.write('LUT_3D_SIZE %d\n' % (steps ** 2))

if False:
steps1 = steps + 1
steps3 = steps ** 2 * (steps + 1)
steps5 = steps ** 4 * (steps + 1)
data = list(in_.getdata())
def lookup(ri, gi, bi):
return data[
ri * steps1 + gi * steps3 + bi * steps5
]
for bi in xrange(steps):
for gi in xrange(steps):
for ri in xrange(steps):
r, g, b = lookup(ri, gi, bi)[:3]
out.write('%f %f %f\n' % (r / 255.0, g / 255.0, b / 255.0))
else:
for pixel in in_.getdata():
r, g, b = pixel[:3]
out.write('%f %f %f\n' % (r / 255.0, g / 255.0, b / 255.0))


if __name__ == '__main__':
main()

或是用 node.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env node

var getPixels = require("get-pixels"),
fs = require('fs'),
program = require('commander');

function haldToCube(hald, output, creator, copyright) {
// "identity.png"
getPixels(hald, function(err, pixels) {

if (err) {
console.log("Bad image path");
return false;
}

var nx = pixels.shape[0],
ny = pixels.shape[1],
index = 0, r = 0, g = 0, b = 0, steps = 0, stream;

if (nx != ny) {
console.log("Hald Image is not square.");
return false;
}

steps = parseInt(Math.round(Math.pow(nx, 1/3)));

console.log("got pixels", pixels.shape.slice());
console.log("steps: ", steps);

// "test.cube"
stream = fs.createWriteStream(output);
stream.once('open', function(fd) {
if (creator) stream.write("#Created by: " + creator + "\n");
if (copyright) stream.write("#Copyright: " + copyright + "\n")
stream.write('LUT_3D_SIZE ' + (steps * steps) + "\n\n");
for(var dx=0; dx<nx; dx++) {
for(var dy=0; dy<ny; dy++) {
r = pixels.data[index++]/255, g = pixels.data[index++]/255, b = pixels.data[index++]/255, a = pixels.data[index++]/255;
// console.log(a);
// if transparent
if (a < 1) {
r = ((1 - a) * r) + (a * r);
g = ((1 - a) * g) + (a * g);
b = ((1 - a) * b) + (a * b);
}
stream.write(parseFloat(r) + " " + parseFloat(g) + " " + parseFloat(b) + "\n");
}
}
stream.end();
});
});
}

program
.version('0.0.1')
.arguments('<hald> <outout>')
.option('-a, --creator [value]', 'Creator information')
.option('-c, --copyright [value]', 'Copyright')
.action(function(hald, output) {
haldToCube(hald, output, program.creator, program.copyright);
});

program.parse(process.argv);

如此,我們就可以將這個色彩效果應用在 Final Cut Pro 中 (via LUT Utility)

如果我們拿到別人的 Hald Image,有時會造成產生出來的 cube 檔太大

我們可以利用 convert 進行縮小作業

先產生預設色彩速查表

convert hald:5 hald.png

將指定的 other_hald 色彩空間套用在我們剛剛產生的預設色彩速查表上,再轉存成 output_hald.png

convert hald.png other_hald.png --hald-clut output_hald.png