如何在图像文件中保存浮点像素值 [英] How to save floating-point pixel values in image file
问题描述
我想将浮点数保存为图像文件中的像素.我目前在OpenCV-python中工作,但我也曾在Pillow(PIL)中尝试过.这两个软件包都将float
像素数据转换为整数,然后再将其写入文件.
I want to save floating-point numbers as pixels in an image file. I am currently working in OpenCV-python, but I had also tried it with Pillow (PIL). Both packages convert float
pixel data to integer before writing them to the file.
我要保存像素值,例如:
I want to save pixel values such as:
(245.7865, 123.18788, 98.9866)
但是当我读回图像文件时,我得到了:
But when I read back the image file I get:
(246, 123, 99)
以某种方式将我的浮点数四舍五入并转换为整数. 如何阻止PIL或OpenCV将其转换为整数?
Somehow my floating-point numbers get rounded off and converted to integers. How to stop PIL or OpenCV from converting them to integer?
推荐答案
光栅图像通常仅存储为整数值.而是直接直接保存numpy数组 像这样
Raster images are normally stored as integer values only. Instead save the numpy array directly like so
x = numpy.array([1, 2, 3])
with open('x.npy', 'wb') as f:
numpy.save(f, x)
然后像这样将变量加载回
Then load the variable back like so
x = numpy.load('x.npy')
其他替代方案包括
- Save one or more GRAY16 png images, with your floats multiplied and truncated.
- Use the Netpbm format supporting floats.
- Save a pickle.
这篇关于如何在图像文件中保存浮点像素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!