首页 > 代码库 > python解析hdr图像文件的python实现

python解析hdr图像文件的python实现

如题

import cv2
import numpy as np
def rgbe2float(rgbe):
    res = np.zeros((rgbe.shape[0],rgbe.shape[1],3))
    p = rgbe[:,:,3]>0
    m = 2.0**(rgbe[:,:,3][p]-136.0)
    res[:,:,0][p] = rgbe[:,:,0][p] * m 
    res[:,:,1][p] = rgbe[:,:,1][p] * m 
    res[:,:,2][p] = rgbe[:,:,2][p] * m
    return res
    
    
def readHdr(fileName = ‘belgium.hdr‘):
    fileinfo = {}
    with open(fileName, ‘rb‘) as fd:
        tline = fd.readline().strip()
        if len(tline)<3 or tline[:2] != ‘#?‘:
            print ‘invalid header‘
            return
        fileinfo[‘identifier‘] = tline[2:]

        tline = fd.readline().strip()
        while tline:
            n = tline.find(‘=‘)
            if n>0:
                fileinfo[tline[:n].strip()] = tline[n+1:].strip()
            tline = fd.readline().strip()

        tline = fd.readline().strip().split(‘ ‘)
        fileinfo[‘Ysign‘] = tline[0][0]
        fileinfo[‘height‘] = int(tline[1])
        fileinfo[‘Xsign‘] = tline[2][0]
        fileinfo[‘width‘] = int(tline[3])

        data = http://www.mamicode.com/[ord(d) for d in fd.read()]>

  

python解析hdr图像文件的python实现