如何在图像文件中保存浮点像素值

Zor*_*dan 5 python opencv image python-imaging-library

我想将浮点数保存为图像文件中的像素。我目前正在使用 OpenCV-python,但我也尝试过使用 Pillow (PIL)。这两个包在将float像素数据写入文件之前都将其转换为整数。

我想保存像素值,例如:

(245.7865, 123.18788, 98.9866)
Run Code Online (Sandbox Code Playgroud)

但是当我读回图像文件时,我得到:

(246, 123, 99)
Run Code Online (Sandbox Code Playgroud)

不知何故,我的浮点数被四舍五入并转换为整数。如何阻止 PIL 或 OpenCV 将它们转换为整数?

vid*_*ige 3

光栅图像通常仅存储为整数值。而是像这样直接保存 numpy 数组

x = numpy.array([1, 2, 3])
with open('x.npy', 'wb') as f:
    numpy.save(f, x)
Run Code Online (Sandbox Code Playgroud)

然后像这样加载变量

x = numpy.load('x.npy')
Run Code Online (Sandbox Code Playgroud)

其他替代方案包括

  • 保存一张或多张 GREY16 png 图像,其中的浮点数相乘并被截断。
  • 使用支持浮点数的Netpbm格式。
  • 留一个泡菜