如何在不将整个图像加载到内存的情况下将单个 tif 图像分段加载到 numpy 数组中?

how can I load a single tif image in parts into numpy array without loading the whole image into memory?

所以有一个 4GB 的 .TIF 图像需要处理,由于内存限制,我无法将整个图像加载到 numpy 数组中,所以我需要从硬盘中部分地延迟加载它。 所以基本上我需要并且需要根据项目要求在 python 中完成。我也尝试在 PyPi tifffile 中寻找 tifffile 库,但我没有找到任何有用的东西,请帮忙。

pyvips 可以做到这一点。例如:

import sys
import numpy as np
import pyvips

image = pyvips.Image.new_from_file(sys.argv[1], access="sequential")

for y in range(0, image.height, 100):
    area_height = min(image.height - y, 100)
    area = image.crop(0, y, image.width, area_height)
    array = np.ndarray(buffer=area.write_to_memory(),
                       dtype=np.uint8,
                       shape=[area.height, area.width, area.bands])

new_from_fileaccess 选项打开顺序模式:pyvips 只会按需从文件中加载像素,限制是您必须从上到下读取像素。

循环 运行 以 100 条扫描线为单位沿着图像向下循环。你当然可以调整这个。

我可以运行这样:

$ vipsheader eso1242a-pyr.tif 
eso1242a-pyr.tif: 108199x81503 uchar, 3 bands, srgb, tiffload_stream
$ /usr/bin/time -f %M:%e ./sections.py ~/pics/eso1242a-pyr.tif
273388:479.50

因此,在这台可悲的旧笔记本电脑上,扫描一张 108,000 x 82,000 像素的图像需要 8 分钟,并且需要 270mb 的内存峰值。

你在做什么处理?你也许可以在 pyvips 中完成所有事情。它比 numpy 快很多。

import pyvips
img = pyvips.Image.new_from_file("space.tif", access='sequential')
out = img.resize(0.01, kernel = "linear")
out.write_to_file("resied_image.jpg")

如果你想将文件转换为其他格式,有更小的尺寸,这段代码就足够了,并且会帮助你在没有任何内存峰值的情况下完成它,并且在非常短的时间内...