首页 > 解决方案 > 如何连接使用内存映射 IO 填充的两个 TIFF 文件

问题描述

我正在尝试编写一个 python 函数,它将在组合多个 TIFF 文件后输出单个 TIFF 文件。我有一个包含大量 TIFF 文件的文件夹,我正在尝试将每个 TIFF 文件加入到一个文件中。我必须将数据加载为 numpy 数组,并且还应该使用内存映射 IO 进行填充。

标签: pythonnumpytiffnumpy-ndarraymemory-mapped-files

解决方案


未经测试的例子,这应该给你一个想法:

from pathlib import Path
import numpy as np
import tifffile

my_path = Path(r'path/to/tiffs')
output = Path('output.tiff')

tiffs = list(my_path.glob('*.tiff'))

x,y = (512,512) # either hardcode or read from first tiff

output = np.zeros((len(tiffs), x, y))

for i, image in enumerate(tiffs):
    a = tifffile.imread(image.open(mode = 'rb'))
    output[i, :, : ] = a

tifffile.imsave(output.open(mode='wb'), output)

推荐阅读