我正在使用 OpenEXR 读取 Python 中的 EXR 文件。我有带有半数据(float16)的 R、G 和 B 通道。我尝试使用 Numpy 将数据从 float16 转换为 uint8 (0-255 颜色),但没有成功。
rCh = getChanEXR(imageFile, 'R','HALF')
rCh = np.array(rCh).astype('uint8')
因此,我将 R 通道像素值放入变量 rCh 中。然后我将 array.array 转换为 np.array,以便我可以使用 astype 方法将其转换为 uint8。我对此很陌生,所以我显然不正确,因为所有值都变成0。最初,这些值是这样的:0.0、2.9567511226945634e-14、1.2295237050707897e-10等。
除了 float16 值之外,我还有一些需要标准化的常规浮点值。我认为我需要先对 float16 值进行标准化,然后才能将它们设置在 0-255 的范围内。
有任何想法吗?谢谢。
添加这里 getChanEXR 中提到的 def 的代码(只是一个基于 python OpenEXR 文档中用于获取通道数据的代码的自定义 def。
def getChanEXR(curEXRStr, curChannel, dataType):
#import OpenEXR, Imath, array
pt = 'none'
if dataType == 'HALF':
pt = Imath.PixelType(Imath.PixelType.HALF)
if dataType == 'FLOAT':
pt = Imath.PixelType(Imath.PixelType.FLOAT)
if dataType == 'UINT':
pt = Imath.PixelType(Imath.PixelType.UINT)
chanstr = OpenEXR.InputFile(curEXRStr).channel(curChannel, pt)
chan = array.array('f', chanstr)
return chan