保存“浮动"的numpy图像 [英] Saving 'float' numpy images

查看:80
本文介绍了保存“浮动"的numpy图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前正在使用的代码:

Here is the code I am currently using:

from PIL import Image
import numpy as np

def save_np_img(np_img, path, name):
    """
    To save the image.
    :param np_img: numpy_array type image
    :param path: string type of the existing path where to save the image
    :param name: string type that includes the format (ex:"bob.png")
    :return: numpy array
    """

    assert isinstance(path, str), 'Path of wrong type! (Must be String)'
    assert isinstance(name, str), 'Name of wrong type! (Must be String)'

    im = Image.fromarray(np_img)
    im.save(path + name)

    return np_img

我希望能够保存包含 float 值的图像,就像我当前可以保存 int 值图像一样.

I would like to be able to save images which contain float values, just like I can currently save int valued-images.

当我尝试保存通过 np.divide(img,255.)进行了转换的图像时,并因此在尝试保存图像时,出现了 TypeError 消息.包含浮点数的numpy_arrays.

I get a TypeError message when I try to save my images which have been transformed through np.divide(img, 255.), and thus when trying to save numpy_arrays which contain floats.

(您也可以提出其他建议).

(You can suggest other librairies, too.)

注意::当值不是0到255之间的整数时,这些值在0到1之间浮动.对于我来说,保持尽可能无损是至关重要的.我最初的想法是我可以简单地使用 np.multiply(img,255),但是我不确定这是否会失去一些精度(实际上也不返回整数).

NOTE: The values, when not integers between 0 and 255, are floats between 0 and 1. It is crucial for me to remain as lossless as possible. My initial thought was that I could simply use np.multiply(img, 255) but I wasn't sure if that would lose some precision (nor if it would return integers, actually).

编辑:基本上,方法是无损转换吗?也就是说,如果我有一个 ints 的numpy_array,将其除以 255.,然后重新转换为 ints ,我会丢失信息吗?如果是这样,如何避免这种情况?

Basically, is this method a lossless convertion? That is, if I had a numpy_array of ints, divided it by 255., and then reconverted into ints, am I losing information? If so, how to avoid that?

推荐答案

您可以无损且原生地保存和读取 numpy 个浮点数组,而无需任何库:

You can save and read back numpy arrays of floats losslessly and natively without any libraries being required:

import numpy as np

# Create 10 random floats in range 0..1 in array "b"
b = np.random.random_sample((10,)).astype(np.float32)

# Save to file
np.save('BunchOfFloats.npy',b)

# Read back into different array "r"
r = np.load('BunchOfFloats.npy')

# Inspect b 
array([0.26565347, 0.7193414 , 0.19435954, 0.58980538, 0.28096624,
   0.88655137, 0.84847042, 0.80156026, 0.94315194, 0.76888901])

# Inspect r
array([0.26565347, 0.7193414 , 0.19435954, 0.58980538, 0.28096624,
   0.88655137, 0.84847042, 0.80156026, 0.94315194, 0.76888901])

可在此处获得文档

或者,按照@WarrenWekesser的评论中的建议,您可以使用TIFF文件,该文件可以存储浮点数,甚至可以存储双精度数.

Alternatively, as suggested in the comments by @WarrenWekesser you could use a TIFF file which can store floats, and even doubles.

import numpy as np
from tifffile import imsave

# Generate float data
b=np.random.random_sample((768,1024,3)).astype(np.float32)

# Save as TIF - when reading, use "data = imread('file.tif')"
imsave('result.tif',b)


另一种选择是 PFM 文件,在此处a>和此处.

这是一种非常简单的格式,您可以自己编写和阅读,并具有其他软件包(例如 ImageMagick GIMP )可以理解的优点,因此您可以保存将数据作为 PFM 文件,然后在命令行中使用 ImageMagick 转换为 JPEG PNG 进行查看:

This is a very simple format that you could write and read yourself, and has the benefit that other packages such as ImageMagick and GIMP understand it, so you could save your data as a PFM file and then convert at the command line with ImageMagick to JPEG or PNG for viewing:

magick image.pfm -auto-level result.png

这篇关于保存“浮动"的numpy图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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