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 将它们转换为整数?
光栅图像通常仅存储为整数值。而是像这样直接保存 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)
其他替代方案包括