└── cube ├── .gitignore ├── a.CUBE ├── cat.jpg └── cube.py /cube/.gitignore: -------------------------------------------------------------------------------- 1 | venv 2 | .idea 3 | -------------------------------------------------------------------------------- /cube/cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krossford/python-use-cube-lut-file-process-image/8212e3c44ea07d00adec02767d5628257b59f335/cube/cat.jpg -------------------------------------------------------------------------------- /cube/cube.py: -------------------------------------------------------------------------------- 1 | from PIL import Image 2 | import math 3 | 4 | 5 | def cubeIndex(r, g, b): 6 | return int(r + g * 32 + b * 32 * 32) 7 | 8 | def mix(a, b, c): 9 | return a + (b - a) * (c - math.floor(c)) 10 | 11 | img = Image.open("cat.jpg") 12 | bitmap = img.load() 13 | 14 | fd = open('a.CUBE') 15 | lines = fd.readlines() 16 | rgbFloatCube = [] 17 | cubeDataStart = False 18 | for l in lines: 19 | if cubeDataStart: 20 | rgbStr = l.split(" ") 21 | if len(rgbStr) == 3: 22 | rgbFloat = (float(rgbStr[0]), float(rgbStr[1]), float(rgbStr[2])) 23 | rgbFloatCube.append(rgbFloat) 24 | if l.startswith("#LUT data points"): 25 | cubeDataStart = True 26 | 27 | print(len(rgbFloatCube)) 28 | 29 | print(img.size) 30 | 31 | for x in range(img.size[0]): 32 | for y in range(img.size[1]): 33 | pixelColor = bitmap[x, y] 34 | red = pixelColor[0] / 255.0 * 31 35 | green = pixelColor[1] / 255.0 * 31 36 | blue = pixelColor[2] / 255.0 * 31 37 | 38 | redH = math.ceil(red) 39 | redL = math.floor(red) 40 | 41 | greenH = math.ceil(green) 42 | greenL = math.floor(green) 43 | 44 | blueH = math.ceil(blue) 45 | blueL = math.floor(blue) 46 | 47 | indexH = cubeIndex(redH, greenH, blueH) 48 | indexL = cubeIndex(redL, greenL, blueL) 49 | 50 | toColorH = rgbFloatCube[indexH] 51 | toColorL = rgbFloatCube[indexL] 52 | 53 | toR = mix(toColorL[0], toColorH[0], red) 54 | toG = mix(toColorL[1], toColorH[1], green) 55 | toB = mix(toColorL[2], toColorH[2], blue) 56 | 57 | toColor2 = (int(toR * 255), int(toG * 255), int(toB * 255)) 58 | bitmap[x, y] = toColor2 59 | 60 | img.show() 61 | 62 | 63 | 64 | --------------------------------------------------------------------------------