Read, process and show the pixels in .EXR format images

4.9k Views Asked by At

I want to read the exr file format images and see the pixel intensities in the corresponding location. And also wanted to stack them together to give them into a neural network. How can I do the normal image processing on these kind of formats? Please help me in doing this!

I have tried this code using OpenEXR file but unable to proceed further.

import OpenEXR
file = OpenEXR.InputFile('file_name.exr')

I am expected to see the normal image processing tools like

file.size()
file.show()
file.write('another format')
file.min()
file.extract_channels()
file.append('another exr file')
2

There are 2 best solutions below

0
On

There is an example on the OpenEXR webpage:

import sys
import array
import OpenEXR
import Imath

if len(sys.argv) != 3:
    print "usage: exrnormalize.py exr-input-file exr-output-file"
    sys.exit(1)

# Open the input file
file = OpenEXR.InputFile(sys.argv[1])

# Compute the size
dw = file.header()['dataWindow']
sz = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)

# Read the three color channels as 32-bit floats
FLOAT = Imath.PixelType(Imath.PixelType.FLOAT)
(R,G,B) = [array.array('f', file.channel(Chan, FLOAT)).tolist() for Chan in ("R", "G", "B") ]

After this, you should have three arrays of floating point data, one per channel. You could easily convert these to numpy arrays and proceed with opencv as user @ZdaR suggests.

1
On

OpenEXR seems to be lacking the fancy image processing features such as displaying images or saving the image to a different format. For this I would suggest you using OpenCV, which is full of image processing features.

What you may need to do is:

  • Read exr using OpenEXR only, then extract channels and convert them to numpy arrays as rCh = np.asarray(rCh, dtype=np.uint8)
  • Create a RGB image from these numpy arrays as img_rgb = cv2.merge([b, g, r]).
  • Use OpenCV functions for your listed operations:
    • Size: img_rgb.shape
    • Show: cv2.imshow(img_rgb)
    • Write: cv2.imwrite("path/to/file.jpg", img_rgb)
    • Min: np.min(b), np.min(g), np.min(r)
    • Extract channels: b, g, r = cv2.split(img_rgb)