pythonopencvopenexr

How to extract and save as separate files the RGBA and Z channels from an EXR file in Python?


Tell me, how I can extract individual channels from an exr image? This feature is in Photoshop and After Effects, but I would like to do it with code. I found the required code here (https://gist.github.com/jadarve/de3815874d062f72eaf230a7df41771b), but it does not save the extracted channels into separate image files


Solution

  • So, if you have img with shape (height, width, 3) and it is float, and in RGB order, you need:

    import cv2
    
    ...
    ... existing code
    ...
    
    # Change to range 0..65535
    img = (img * 65535).astype(np.uint16)
    # Change from RGB to BGR ordering
    imgBGR = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    # Save to disk
    cv2.imwrite('result.png', imgBGR)
    
    # Now do,likewise for depth data
    z = (z * 65535).astype(np.uint16)
    cv2.imwrite('z.png', z)
    

    If you have RGBA, use cv2.COLOR_RGBA2BGRA