exr2png.py 1.42 KB
Newer Older
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
import OpenEXR
import Imath
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from PIL import Image

# From here: https://gist.github.com/arseniy-panfilov/4dc8fc5131277affe64619b1a9d00da0
def exr2arr(exrfile):
    file = OpenEXR.InputFile(exrfile)
    dw = file.header()['dataWindow']

    channels = file.header()['channels'].keys()
    channels_list = list()
    for c in ('R', 'G', 'B', 'A'):
        if c in channels:
            channels_list.append(c)

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
19
    # the shape had incorrect order
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
20 21 22 23 24
    #size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)
    size = (dw.max.y - dw.min.y + 1, dw.max.x - dw.min.x + 1)
    color_channels = file.channels(channels_list, Imath.PixelType(Imath.PixelType.FLOAT))
    channels_tuple = [np.fromstring(channel, dtype='f') for channel in color_channels]
    res = np.dstack(channels_tuple)
25
    return res.reshape(size + (len(channels_tuple),))
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
26 27 28


if __name__ == "__main__":
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
29

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
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
    fname = "depth-0001.exr"
    arr = exr_to_array(fname)
    print(arr.shape)

    dmap = arr[:,:,0]
    print(np.max(dmap))

    dmap[dmap==65504]=0
    dmap[dmap==10000000000.0]=0

    print(np.max(dmap))

    k = 1
    #k = 0xffff/np.max(dmap)
    #k = 5 # 20cm
    k = 100 # 1 cm
    #k = 10 # 10cm /unit

    print("scale = "+str(k))
    dmap = k*dmap
    dmap[dmap>65535]=0

    im = Image.fromarray(dmap.astype(np.uint16))
    im.save('model-depth.png')

    plt.imshow(dmap)
    plt.colorbar()
    plt.show()