【问题标题】:Saving .tif stack with PIL/Image使用 PIL/Image 保存 .tif 堆栈
【发布时间】:2020-02-17 12:56:46
【问题描述】:

我在一个目录中有一堆 .tif 图像,我正在尝试使用 PIL 打开它们并将它们保存为单个 .tif 文件,其中每个图像都是一个框架。根据这个原则上应该是可能的:https://github.com/python-pillow/Pillow/issues/3636#issuecomment-508058396

到目前为止,我得到了:

from PIL import Image

img_frames = ['test_imgs/img1.tif',
            'test_imgs/img2.tif',
            'test_imgs/img3.tif']

# read the images and store them in a list
ordered_image_files = []
for img in img_frames:
    with Image.open(img) as temp_img:
        ordered_image_files.append(temp_img)

# save the first image and add the rest as frames
ordered_image_files[0].save('test.tif', 
                            save_all = True, 
                            append_images = ordered_image_files[1:])

运行它给了我:

AttributeError: 'TiffImageFile' object has no attribute 'load_read'

如果我打印我得到的图像对象及其类:

> print(ordered_image_files[0])
<PIL.TiffImagePlugin.TiffImageFile image mode=I;16B size=512x512 at 0x1028C6550>
> print(type(ordered_image_files[0]))
<class 'PIL.TiffImagePlugin.TiffImageFile'>

所以我认为阅读部分很好。

我是图像处理的新手,所以我可能遗漏了一些明显的东西。

提前致谢。

完整的错误跟踪:

Traceback (most recent call last):
  File "/Users/mpages/miniconda3/envs/denoise_n2v/lib/python3.6/site-packages/PIL/ImageFile.py", line 161, in load
    read = self.load_read
AttributeError: 'TiffImageFile' object has no attribute 'load_read'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "img_test.py", line 24, in <module>
    append_images = ordered_image_files[1:])
  File "/Users/mpages/miniconda3/envs/denoise_n2v/lib/python3.6/site-packages/PIL/Image.py", line 2050, in save
    self._ensure_mutable()
  File "/Users/mpages/miniconda3/envs/denoise_n2v/lib/python3.6/site-packages/PIL/Image.py", line 640, in _ensure_mutable
    self._copy()
  File "/Users/mpages/miniconda3/envs/denoise_n2v/lib/python3.6/site-packages/PIL/Image.py", line 633, in _copy
    self.load()
  File "/Users/mpages/miniconda3/envs/denoise_n2v/lib/python3.6/site-packages/PIL/TiffImagePlugin.py", line 1098, in load
    return super(TiffImageFile, self).load()
  File "/Users/mpages/miniconda3/envs/denoise_n2v/lib/python3.6/site-packages/PIL/ImageFile.py", line 165, in load
    read = self.fp.read
AttributeError: 'NoneType' object has no attribute 'read'

【问题讨论】:

  • 请显示完整的错误跟踪。
  • 如果你想再看一遍,我更新了我的答案。

标签: python python-3.x image python-imaging-library


【解决方案1】:

我相信您的 with 声明正在关闭您的图像文件或将其置于可以访问的内存上下文之外,因此您可以尝试在我的 Mac 上正常工作:

for img in img_frames: 
    ordered_image_files.append(Image.open(img)) 

失败了,我在 tifffile 模块上取得了一些成功,如下所示:

import tifffile
img_frames = [ '1.tif', '2.tif', '3.tif' ]

with tifffile.TiffWriter('multipage.tif') as stack: 
    for filename in img_frames: 
        stack.save(tifffile.imread(filename))

关键字:Python、TIF、TIFF、tifffile、多页、多页、序列、图像、图像处理

【讨论】:

  • 感谢您的回答。我已经尝试了您的建议,但是对于我的真实图像数据集,我有超过 3 张图像(~400)。这给出了这个错误:OSError: [Errno 24] Too many open files:。所以我想我需要在某个时候关闭文件,这就是 with 声明的原因。
  • 如果你在 macOS 或 Linux 上,你应该可以增加打开文件描述符的限制,比如ulimit -n 2048试试help ulimit
  • 我没有尝试增加ulimit,但使用tifffile 库确实有效,非常感谢!
猜你喜欢
  • 2012-11-28
  • 1970-01-01
  • 2011-06-23
  • 1970-01-01
  • 2018-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
相关资源
最近更新 更多