Skip to content

Instantly share code, notes, and snippets.

@walsvid
Forked from jadarve/readEXR.py
Created January 14, 2019 14:28
Show Gist options
  • Save walsvid/c8e5b0114e70762fd788d4ae15e25722 to your computer and use it in GitHub Desktop.
Save walsvid/c8e5b0114e70762fd788d4ae15e25722 to your computer and use it in GitHub Desktop.
Extract RGB and depth channels from exr images.
import numpy as np
import OpenEXR as exr
import Imath
def readEXR(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', 'Z']
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, channelData['Z']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment