将浮点数组保存到图像(EXR 格式) [英] Save float array to image (with EXR format)

查看:175
本文介绍了将浮点数组保存到图像(EXR 格式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码不起作用,它在将图像写入磁盘之前将值转换为 np.uint8.

The following code does not work, it converts values to np.uint8 before writing image to disk.

import cv2
import numpy as np
# Generate dummy gradient with float values
arr = np.arange(0,10,0.02)
arr = np.repeat(arr, arr.shape[0])
arr.reshape((500,500))
cv2.imwrite('output.exr',arr)
# At this point, returns True. Opening the image with OpenEXR 1.4 shows values have become UINT8 instead of Float
arr = cv2.imread('output.exr')
# Shape is (1000, 1000, 3)
print(arr[10,10])
array([0, 0, 0], dtype=uint8) # All float data is lost

作为一点奖励,文档非常没有帮助

As a little bonus, the documentation is very not helpful

$ pydoc cv2.imwrite
Help on built-in function imwrite in cv2:

cv2.imwrite = imwrite(...)
    imwrite(filename, img[, params]) -> retval

没有说明参数应该是什么...

Doesn't say what the params should be...

如何将带有浮点值的数组保存为 EXR 格式?(使用 OpenCV)

How can I save an array with floating point values to EXR format ? (using OpenCV)

推荐答案

imageio 可以保存 EXR任何数据格式的文件,(它还支持大量其他图像格式).

imageio can save EXR files in any data format, (and it also supports a large amount of other image formats).

读取和写入大多数图像几乎可以挽救生命:

Pretty much a life saver for reading and writing most images:

import numpy as np
import imageio

# Generate dummy random image with float values
arr = np.random.uniform(0.0, 1.0, size=(500,500))

# freeimage lib only supports float32 not float64 arrays
arr = arr.astype("float32")

# Write to disk
imageio.imwrite('float_img.exr', arr)

# Read created exr from disk
img = imageio.imread('float_img.exr')

assert img.dtype == np.float32

这篇关于将浮点数组保存到图像(EXR 格式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆