Created
March 4, 2014 12:48
-
-
Save hi2p-perim/9345872 to your computer and use it in GitHub Desktop.
Load Radiance HDR file (.hdr) with python. Used : smc.freeimage, Pillow, OpenCV
This file contains hidden or 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 array | |
import smc.freeimage as fi | |
from PIL import Image | |
import numpy as np | |
import cv2 | |
def gamma_correction(a, gamma): | |
return np.power(a, 1/gamma) | |
def load_hdr(file): | |
img = fi.Image(file).flipVertical() | |
size = img.height * img.pitch | |
raw = img.getRaw() | |
floats = array.array('f', raw) | |
a1 = np.array(floats).reshape((img.width, img.height, 3)) | |
a2 = np.clip(gamma_correction(a1, 2.2), 0, 1) | |
return (a2 * 255).astype(np.uint8) | |
def save_image(file, image): | |
img2 = Image.fromarray(image) | |
img2.save(file) | |
def main(): | |
image = load_hdr('grace_probe.hdr') | |
save_image('grace_probe.png', image) | |
cv2.imshow('grace_probe', cv2.cvtColor(image, cv2.COLOR_RGB2BGR)) | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is this for reading medical image .hdr file?