如何逐个补丁地写入png/tiff文件?

ass*_*sin 10 python python-imaging-library pillow

我想从一个非常大的h5py数据集创建一个png或tiff图像文件,该数据集不能一次加载到内存中.所以,我想知道在python中是否有办法在补丁中写入png或tiff文件?(我可以将切片中的h5py数据集加载到a numpy.ndarray).我已经尝试使用枕头库并PIL.Image.paste给出了盒子坐标,但对于大图像,它会耗尽内存.

基本上,我想知道是否有办法做一些事情:

for y in range(0, height, patch_size):
    for x in range(0, width, patch_size):
        y2 = min(y + patch_size, height)
        x2 = min(x + patch_size, width)
        # image_arr is an h5py dataset that cannot be loaded completely
        # in memory, so load it in slices
        image_file.write(image_arr[y:y2, x:x2], box=(y, x, y2, x2))
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种方法来做到这一点,而不是将整个图像加载到内存中.我已经尝试过枕头库,但它会将所有数据加载/保存在内存中.

编辑:这个问题不是关于h5py,而是关于如何将非常大的图像(无法加载到内存中)写入补丁中的文件 - 类似于通过逐行写入来构建大文本文件的方式.

cgo*_*lke 2

尝试tifffile.memmap

from tifffile import memmap

image_file = memmap('temp.tif', shape=(height, width), dtype=image_arr.dtype,
                    bigtiff=True)

for y in range(0, height, patch_size):
    for x in range(0, width, patch_size):
        y2 = min(y + patch_size, height)
        x2 = min(x + patch_size, width)
        image_file[y:y2, x:x2] = image_arr[y:y2, x:x2]

image_file.flush()
Run Code Online (Sandbox Code Playgroud)

这将创建一个带有一条带的未压缩 BigTIFF 文件。内存映射图块尚未实现。不确定有多少库可以处理此类文件,但您始终可以使用 TIFF 标记中的元数据直接从条带中读取。