简体   繁体   English

如何保存模式“F”图像? (Python的/ PIL)

[英]How do I save a mode 'F' image? (Python/PIL)

I have a ndarray with floats in it I want to save. 我有一个带浮动的ndarray,我想保存。 I would like to keep the values as float though. 我想将值保持为浮点数。 The only format I found that accepts saving float data is tiff. 我发现接受保存浮点数据的唯一格式是tiff。 Doesn't show the actual image however. 但是不显示实际图像。

from Image import *
from numpy import *

img = random.random((300, 300)) #float numbers, i have actual data in my image though
img = fromarray(img)
img.save('test.tiff')

Your example is saving a floating-point TIFF file. 您的示例保存浮点TIFF文件。 I've confirmed by examining the TIFF header, noting that the samples per pixel tag 0x153 has a value of 3 (floating point data). 我已通过检查TIFF标头确认,注意每像素标签0x153的样本值为3(浮点数据)。 Using your example: 使用你的例子:

import Image
from numpy import *

data = random.random((2, 2))
img1 = Image.fromarray(data)
img1.save('test.tiff')
img2 = Image.open('test.tiff')

f1 = list(img1.getdata())
f2 = list(img2.getdata())
print f1 == f2
print f1

Output: 输出:

True
[0.27724304795265198, 0.12728925049304962, 0.4138914942741394, 0.57919681072235107]

Details on the TIFF6 file format 有关TIFF6文件格式的详细信息

Updated : Example 64x64 image viewed on Mac desktop: 更新 :在Mac桌面上查看的示例64x64图像: 在此输入图像描述

ImageJ打开浮动Tiff图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM