Python version for ints
def save2grid(filename,volume,dimx,dimy,dimz):
with open(filename, "wb") as file:
for i in range(dimx):
for j in range(dimy):
for k in range(dimz):
f = (volume[i][j][k]*255).astype(np.uint8) #numpy.uint8
#print(type(f))
b = struct.pack('B', f) #was f
file.write(b)
def save2gridfast(filename,volume,dimx,dimy,dimz):
flat=volume.reshape((dimx*dimy*dimz))
flat=(flat*255).astype(np.uint8)
with open(filename, "wb") as file:
b = struct.pack('%sB' % len(flat), *flat)
file.write(b)
Python version for floats
def save2grid(filename,volume,dimx,dimy,dimz):
with open(filename, "wb") as file:
for i in range(dimx):
for j in range(dimy):
for k in range(dimz):
f = volume[i][j][k] #[3.14, 2.7, 0.0, -1.0, 1.1]
b = struct.pack('f', f)
file.write(b)
def save2grid2(filename,volume,dimx,dimy,dimz):
flat=volume.reshape((dimx*dimy*dimz))
with open(filename, "wb") as file:
for i in range(dimx*dimy*dimz):
f = flat[i]
b = struct.pack('f', f)
file.write(b)
def save2grid3(filename,volume,dimx,dimy,dimz):
flat=volume.reshape((dimx*dimy*dimz))
with open(filename, "wb") as file:
b = struct.pack('%sf' % len(flat), *flat)
#b = struct.pack('f', f)
file.write(b)