-
-
Save walsvid/c8e5b0114e70762fd788d4ae15e25722 to your computer and use it in GitHub Desktop.
Extract RGB and depth channels from exr images.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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