问题描述
我想使用TIFF图像来有效地保存大量的测量数据.通过将它们设置为mode ="I; 16"(对应于我的16位数据范围),它们产生2MB的文件(〜1000x1000像素").哪个好.
I want to use TIFF images to effectively save large arrays of measurement data. With setting them to mode="I;16" (corresponding to my 16 bit data range), they yield 2MB files (~1000x1000 "pixel"). Which is good.
但是,在分析它们时,我很难将它们重新转换为数组.对于32位数据(->"I"),numpy.array命令可以正常工作.在"I; 16"的情况下,结果是一个TIFF作为[0,0]条目的0D numpy数组.
However I am having troubles reconverting them into arrays when it comes to analysing them. For 32bit data (-> "I") the numpy.array command works fine. In case of "I;16" the result is a 0D numpy array with the TIFF as the [0,0] entry.
是否有办法使它正常工作?我真的想避免使用32位图像,因为我不需要范围,它使所需的HDD空间增加了一倍(计划中的很多此类测量工作……)
Is there a way to get that to work? I would really like to avoid using 32bit images, as I don't need the range and it doubles the HDD space required (lots and lots of those measurements planned...)
推荐答案
这应该有效(枕头/PIL解决方案,对于16位图像较慢,请参见下文).
This should work (pillow/PIL solution, slow for 16-bit image, see below).
from PIL import Image
import numpy as np
data = np.random.randint(0,2**16-1,(1000,1000))
im = Image.fromarray(data)
im.save('test.tif')
im2 = Image.open('test.tif')
data2 = np.array(im2.getdata()).reshape(im2.size[::-1])
另一种使用C. Gohlke的 tifffile 的解决方案(非常快):
Another solution using tifffile by C. Gohlke (very fast):
import tifffile
fp = r'path\to\image\image.tif'
with tifffile.TIFFfile(fp) as tif:
data = tif.asarray()
这篇关于I"16"的numpy.array.图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!