Dawei Yang
Dawei Yang

Reputation: 626

How to load *.hdr files using python

I would like to read an environment map in *.hdr file format. It seems that very popular libraries doesn't support .hdr file reading, for example, OpenCV, PIL etc.. So how to read a .hdr file into a numpy array?

Upvotes: 7

Views: 17689

Answers (3)

Ivan Tishchenko
Ivan Tishchenko

Reputation: 106

Libraries such as OpenCV, ImageIO etc. support loading of radiance RGBE (.hdr).

For example using OpenCV:

import cv2

# IMREAD_ANYDEPTH is needed because even though the data is stored in 8-bit channels
# when it's read into memory it's represented at a higher bit depth
img = cv2.imread(hdr_path, flags=cv2.IMREAD_ANYDEPTH)

Reference:

Upvotes: 0

solisoares
solisoares

Reputation: 25

For some reason when I was trying to load a MRI image in .hdr format using format='HDR-FI' it was returning Could not load bitmap <path to image>: : RGBE read error

But if you type imageio.show_formats() it returns a list of formats including "ITK - Insight Segmentation and Registration Toolkit", where it shows that it can handle .hdr images as well.

So my alternative was to use:

pip install itk

hdr_path = "<path to image>"
img = imageio.imread(hdr_path, 'ITK') # returns a tuple

img = np.array(img) # transforms to numpy array

Upvotes: 0

Dawei Yang
Dawei Yang

Reputation: 626

I found ImageIO very useful. It can handle many image file formats including .hdr images. Here is the list: ImageIO Formats

It can be easily installed using easy_install or pip.

Upvotes: 7

Related Questions