my question is: I've a Square Hald 8 512x512px, and I would to convert it to .CUBE file
- Convert Square Hald to Classic Hald and then convert to CUBE easily(see 2nd alghorithm) Pics: Square Hald 8 - Classic Hald 8
I'm trying to reverse this simple python alghorithm that works fine to convert Classic Hald -> Square Hald:
import numpy, cv2 hald = cv2.imread("classic_hald_8.png") size = int(hald.shape[0] ** (1.0/3.0) + .5) clut = numpy.concatenate([ numpy.concatenate(hald.reshape((size,size,size**2,size**2, 3))[row], axis=1) for row in range(size) ]) cv2.imwrite("square_hald_8.png", clut)
import imageio as iio, numpy as np imagen=iio.imread("Classic_Hald_8.png") r,g,b=(imagen[:,:,0]).reshape(-1), (imagen[:,:,1]).reshape(-1), (imagen[:,:,2]).reshape(-1) np.savetxt("Classic_Hald_8.cube",X=np.column_stack((r/255,g/255,b/255)),fmt='%1.6f', header="LUT_3D_SIZE 64", comments="")
We need to reshape the values. Some ideas or suggests? thanks
UPDATE2: I've solved thanks to Eugene Vdovin, GrossGrade author. If someone has suggests to implement a 3D structure(maybe with numpy?) are well accepted. I'm very noob with python
from PIL import Image
im = Image.open('test.png','r')
values = im.load()
hald_side_in_pixels = im.size[0]
hald_in_pixels = im.size[0]*im.size[0]
lutSize = int(hald_in_pixels ** (1.0/3.0) + .5)
fr = [0.0]*hald_in_pixels
fg = [0.0]*hald_in_pixels
fb = [0.0]*hald_in_pixels
cubeIndex = 0
for y in range(hald_side_in_pixels):
for x in range(hald_side_in_pixels):
iR = cubeIndex % lutSize
iG = y % lutSize
iB = int(x/lutSize)+(int(y/lutSize)*int(hald_side_in_pixels/lutSize))
idx = iR * lutSize * lutSize + iG * lutSize + iB
fr[idx],fg[idx],fb[idx] = values[x,y]
cubeIndex+=1
with open("test.cube", "w") as output:
output.write("DOMAIN_MIN 0 0 0\nDOMAIN_MAX 1 1 1\nLUT_3D_SIZE " + str(lutSize) + '\n')
for iB in range(lutSize):
for iG in range(lutSize):
for iR in range(lutSize):
idx = iR * lutSize * lutSize + iG * lutSize + iB
output.write((str("%.9f" % (fr[idx]/255)) + ' ' + str("%.9f" % (fg[idx]/255)) + ' ' + str("%.9f" % (fb[idx]/255)))+ '\n')
output.close()
UPDATE3: I've followed the suggest, I've created a 3D array with numpy, it's cleaner now, but it is about 150ms slower than 3x 1D array, I post the code
from PIL import Image
import numpy as np
im = Image.open('test.png','r')
values = im.load()
hald_side_in_pixels = im.size[0]
lutSize = int((hald_side_in_pixels*hald_side_in_pixels) ** (1.0/3.0) + .5)
LUT = np.empty((lutSize,lutSize,lutSize), dtype=bytearray)
cubeIndex = 0
for y in range(hald_side_in_pixels):
for x in range(hald_side_in_pixels):
iR = cubeIndex % lutSize
iG = y % lutSize
iB = int(x/lutSize)+(int(y/lutSize)*int(hald_side_in_pixels/lutSize))
LUT[iR,iG,iB]=values[x,y]
cubeIndex+=1
with open("test1.cube", "w") as output:
output.write("DOMAIN_MIN 0 0 0\nDOMAIN_MAX 1 1 1\nLUT_3D_SIZE " + str(lutSize) + '\n')
for iB in range(lutSize):
for iG in range(lutSize):
for iR in range(lutSize):
output.write((str("%.9f" % (LUT[iR,iG,iB][0]/255)) + ' ' + str("%.9f" % (LUT[iR,iG,iB][1]/255)) + ' ' + str("%.9f" % (LUT[iR,iG,iB][2]/255)))+ '\n')
output.close()