lijiankuan
10/31/2019 - 2:09 AM

读取 OpenEXR 文件

import numpy as np
import OpenEXR as exr
import Imath

def readRGB(filename):
    """Read RGB + Depth data from EXR image file.
    Parameters
    ----------
    filename : str
        File path.
    Returns
    -------
    img : RGB image in float32 format.
    Z : Depth buffer in float3.
    """
    
    exrfile = exr.InputFile(filename)
    dw = exrfile.header()['dataWindow']
    isize = (dw.max.y - dw.min.y + 1, dw.max.x - dw.min.x + 1)
    
    channels = ['R', 'G', 'B']
    channelData = dict()
    
    for c in channels:
        C = exrfile.channel(c, Imath.PixelType(Imath.PixelType.FLOAT))
        C = np.fromstring(C, dtype=np.float32)
        C = np.reshape(C, isize)
        
        channelData[c] = C
    
    
    # create RGB image
    img = np.concatenate([channelData[c][...,np.newaxis] for c in ['R', 'G', 'B']], axis=2)
    
    
    return img