如何保存模式“F”图像? (Python的/ PIL) [英] How do I save a mode 'F' image? (Python/PIL)

查看:726
本文介绍了如何保存模式“F”图像? (Python的/ PIL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带浮动的ndarray,我想保存。我想将值保持为浮点数。我发现接受保存浮点数据的唯一格式是tiff。但是不显示实际图像。

I have a ndarray with floats in it I want to save. I would like to keep the values as float though. The only format I found that accepts saving float data is 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')


推荐答案

您的示例保存浮点TIFF文件。我通过检查TIFF标头确认,注意每个像素的样本标签0x153的值为3(浮点数据)。使用您的示例:

Your example is saving a floating-point TIFF file. I've confirmed by examining the TIFF header, noting that the samples per pixel tag 0x153 has a value of 3 (floating point data). 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

输出:

True
[0.27724304795265198, 0.12728925049304962, 0.4138914942741394, 0.57919681072235107]

有关TIFF6文件格式的详细信息

已更新:在Mac桌面上查看的示例64x64图片:

Updated: Example 64x64 image viewed on Mac desktop:

这篇关于如何保存模式“F”图像? (Python的/ PIL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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