如何在图像文件中保存浮点像素值 [英] How to save floating-point pixel values in image file

查看:168
本文介绍了如何在图像文件中保存浮点像素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将浮点数保存为图像文件中的像素.我目前在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')

其他替代方案包括

  • 保存一个或多个GREY16 png图像,并将您的花车倍增并截短.
  • 使用 Netpbm 支持浮点数的格式.
  • 保存 pickle .
  • Save one or more GRAY16 png images, with your floats multiplied and truncated.
  • Use the Netpbm format supporting floats.
  • Save a pickle.

这篇关于如何在图像文件中保存浮点像素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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