import sys
from matplotlib import image as mpimg
import numpy as np
import os

DIPHA_CONST = 8067171840
DIPHA_IMAGE_TYPE_CONST = 1
DIM = 3

input_dir = os.path.join(os.getcwd(), sys.argv[1])
dipha_output_filename = sys.argv[2]
vert_filename = sys.argv[3]

input_filenames = [name
                   for name in os.listdir(input_dir)
                   if (os.path.isfile(input_dir + '/' + name)) and (name != ".DS_Store")]
input_filenames.sort()


image = mpimg.imread(os.path.join(input_dir, input_filenames[0]))
nx, ny = image.shape
del image
nz = len(input_filenames)

print(nx, ny, nz)
#sys.exit()
im_cube = np.zeros([nx, ny, nz])

i = 0
for name in input_filenames:
    sys.stdout.flush()
    print(i, name)
    fileName = input_dir + "/" + name
    im_cube[:, :, i] = mpimg.imread(fileName)
    i = i + 1


print('writing dipha output...')
with open(dipha_output_filename, 'wb') as output_file:
    # this is needed to verify you are giving dipha a dipha file
    np.int64(DIPHA_CONST).tofile(output_file)
    # this tells dipha that we are giving an image as input
    np.int64(DIPHA_IMAGE_TYPE_CONST).tofile(output_file)
    # number of points
    np.int64(nx * ny * nz).tofile(output_file)
    # dimension
    np.int64(DIM).tofile(output_file)
    # pixels in each dimension
    np.int64(nx).tofile(output_file)
    np.int64(ny).tofile(output_file)
    np.int64(nz).tofile(output_file)
    # pixel values
    for k in range(nz):
        sys.stdout.flush()
        print('dipha - working on image', k)
        for j in range(ny):
            for i in range(nx):
                val = int(-im_cube[i, j, k]*255)
                '''
                if val != 0 and val != -1:
                    print('val check:', val)
                '''
                np.float64(val).tofile(output_file)
    output_file.close()

print('writing vert file')
with open(vert_filename, 'w') as vert_file:
    for k in range(nz):
        sys.stdout.flush()
        print('verts - working on image', k)
        for j in range(ny):
            for i in range(nx):
                vert_file.write(str(i) + ' ' + str(j) + ' ' + str(k) + ' ' + str(int(-im_cube[i, j, k] * 255)) + '\n')
    vert_file.close()

print(nx, ny, nz)

[((638, 660), 'numpy.zeros', 'np.zeros', (['[nx, ny, nz]'], {}), '([nx, ny, nz])\n', (646, 660), True, 'import numpy as np\n'), ((174, 185), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (183, 185), False, 'import os\n'), ((488, 531), 'os.path.join', 'os.path.join', (['input_dir', 'input_filenames[0]'], {}), '(input_dir, input_filenames[0])\n', (500, 531), False, 'import os\n'), ((705, 723), 'sys.stdout.flush', 'sys.stdout.flush', ([], {}), '()\n', (721, 723), False, 'import sys\n'), ((807, 829), 'matplotlib.image.imread', 'mpimg.imread', (['fileName'], {}), '(fileName)\n', (819, 829), True, 'from matplotlib import image as mpimg\n'), ((325, 346), 'os.listdir', 'os.listdir', (['input_dir'], {}), '(input_dir)\n', (335, 346), False, 'import os\n'), ((1499, 1517), 'sys.stdout.flush', 'sys.stdout.flush', ([], {}), '()\n', (1515, 1517), False, 'import sys\n'), ((1995, 2013), 'sys.stdout.flush', 'sys.stdout.flush', ([], {}), '()\n', (2011, 2013), False, 'import sys\n'), ((371, 409), 'os.path.isfile', 'os.path.isfile', (["(input_dir + '/' + name)"], {}), "(input_dir + '/' + name)\n", (385, 409), False, 'import os\n'), ((1010, 1031), 'numpy.int64', 'np.int64', (['DIPHA_CONST'], {}), '(DIPHA_CONST)\n', (1018, 1031), True, 'import numpy as np\n'), ((1118, 1150), 'numpy.int64', 'np.int64', (['DIPHA_IMAGE_TYPE_CONST'], {}), '(DIPHA_IMAGE_TYPE_CONST)\n', (1126, 1150), True, 'import numpy as np\n'), ((1200, 1222), 'numpy.int64', 'np.int64', (['(nx * ny * nz)'], {}), '(nx * ny * nz)\n', (1208, 1222), True, 'import numpy as np\n'), ((1265, 1278), 'numpy.int64', 'np.int64', (['DIM'], {}), '(DIM)\n', (1273, 1278), True, 'import numpy as np\n'), ((1336, 1348), 'numpy.int64', 'np.int64', (['nx'], {}), '(nx)\n', (1344, 1348), True, 'import numpy as np\n'), ((1374, 1386), 'numpy.int64', 'np.int64', (['ny'], {}), '(ny)\n', (1382, 1386), True, 'import numpy as np\n'), ((1412, 1424), 'numpy.int64', 'np.int64', (['nz'], {}), '(nz)\n', (1420, 1424), True, 'import numpy as np\n'), ((1825, 1840), 'numpy.float64', 'np.float64', (['val'], {}), '(val)\n', (1835, 1840), True, 'import numpy as np\n')]