将大型多层TIFF导入为NumPy阵列

我正在尝试将3D TIFF图像加载为NumPy数组。我无法为大型索引加载切片,并且最大索引大小取决于数组的大小。例如,使用2048x2048图像堆栈,我无法在256th = 2 ^ 8th之后加载图像。对于1024x1024的堆栈,我无法在1024 = 2 ^ 10th之后加载图像。也就是说,无论哪种情况,我都只能访问TIF图像的前2 ^ 31个字节。请注意,我只是尝试从堆栈中加载单个图像。

我在具有8Gb RAM的计算机上的Windows 10上使用python 3.7.5,NumPy 1.17.4版和PIL 6.2.1版。

再现问题的最小示例:

import numpy as np
from PIL import Image

# generate test image
fname = r"test.tif"
img = np.random.randint(2**16,size=(257,2048,2048),dtype=np.uint16)

# save test image
im_list = [Image.fromarray(img[ii].astype('uint16')) for ii in range(img.shape[0])]
im_list[0].save(fname,save_all=True,append_images=im_list[1:])

# load image
im_loaded = Image.open(fname)

# this succceeds
im_loaded.seek(255)
arr255 = np.asarray(im_loaded,dtype=np.uint16)

# this fails
im_loaded.seek(256)
arr256 = np.asarray(im_loaded,dtype=np.uint16)
Traceback (most recent call last):
  File "C:\Program Files\Python\Python37\lib\site-packages\IPython\core\interactiveshell.py",line 3319,in run_code
    exec(code_obj,self.user_global_ns,self.user_ns)
  File "<ipython-input-107-3430f5720c7e>",line 2,in <module>
    arr256 = np.asarray(im_loaded,dtype=np.uint16)
  File "C:\Program Files\Python\Python37\lib\site-packages\numpy\core\_asarray.py",line 85,in asarray
    return array(a,dtype,copy=False,order=order)
TypeError: int() argument must be a string,a bytes-like object or a number,not 'TiffImageFile'

这似乎是PIL问题,因为我可以使用其他工具成功读取TIFF文件。特别是libtiff

import libtiff
tif = libtiff.TIFF.open(fname,'r')
for ii,tif_slice in enumerate(tif.iter_images()):
    if ii == 256:
        arr = np.asarray(tif_slice,dtype=np.uint16)
lxqcys 回答:将大型多层TIFF导入为NumPy阵列

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2860015.html

大家都在问