【问题标题】:Python PIL For Loop to work with Multi-image TIFFPython PIL For Loop 与多图像 TIFF 一起使用
【发布时间】:2019-11-11 16:38:12
【问题描述】:

每个 tiff 文件中有 4 个图像。如果可能,我不希望提取和保存它们,我只想使用 for 循环来查看它们中的每一个。 (比如查看像素 [0,0] )并根据它在所有 4 中的颜色,我会做相应的事情。

这可以使用 PIL 吗? 如果不是我应该使用什么。

【问题讨论】:

    标签: python python-imaging-library tiff


    【解决方案1】:

    可以使用PIL.ImageSequence(实际上等效于source code)来遍历图像页面,而不是循环直到EOFError

    from PIL import Image, ImageSequence
    
    im = Image.open("multipage.tif")
    
    for i, page in enumerate(ImageSequence.Iterator(im)):
        page.save("page%d.png" % i)
    

    【讨论】:

      【解决方案2】:

      您可以使用 PIL 图像的“seek”方法来访问 tif 的不同页面(或动画 gif 的帧)。

      from PIL import Image
      
      img = Image.open('multipage.tif')
      
      for i in range(4):
          try:
              img.seek(i)
              print img.getpixel( (0, 0))
          except EOFError:
              # Not enough frames in img
              break
      

      【讨论】:

      • 仅供参考,该解决方案的工作速度比 pim 库(0.348093 秒)快 1000 倍,以便稍后访问帧中的单个像素(0.0002 秒)。 pims 应该是基于 tiffile.py 的。 PIL 仍然比 opencv 慢(6.7e-05sec),但 opencv 不支持多页 tiff(tiff 堆栈)。
      【解决方案3】:

      今天必须做同样的事情,

      我按照@stochastic_zeitgeist 的代码进行了改进(不要手动循环读取每个像素)以加快速度。

      from PIL import Image
      import numpy as np
      
      def read_tiff(path):
          """
          path - Path to the multipage-tiff file
          """
          img = Image.open(path)
          images = []
          for i in range(img.n_frames):
              img.seek(i)
              images.append(np.array(img))
          return np.array(images)
      

      【讨论】:

        【解决方案4】:

        这是一个读取多页 tiff 并将图像作为 numpy 数组返回的方法

        from PIL import Image
        import numpy as np
        
        def read_tiff(path, n_images):
            """
            path - Path to the multipage-tiff file
            n_images - Number of pages in the tiff file
            """
            img = Image.open(path)
            images = []
            for i in range(n_images):
                try:
                    img.seek(i)
                    slice_ = np.zeros((img.height, img.width))
                    for j in range(slice_.shape[0]):
                        for k in range(slice_.shape[1]):
                            slice_[j,k] = img.getpixel((j, k))
        
                    images.append(slice_)
        
                except EOFError:
                    # Not enough frames in img
                    break
        
            return np.array(images)
        

        【讨论】:

        • PIL/Pillow 使用 [col,row] 存储和引用图像,而 numpy 使用 [row,col]。我通过更改高度和宽度位置使上述代码工作,即slice_ = np.zeros((img.width, img.height))
        • TIFF 文件中的帧数存储在n_frames 属性中。此外,要将它们转换为 numpy 数组,您不需要复制每个单独的像素 - 就像 numpy.array(image) 一样简单,其中 image 是 PIL 图像。
        【解决方案5】:

        感谢这个线程上的答案,我编写了这个 python 模块,用于读取和操作多页 tiff 文件:https://github.com/mpascucci/multipagetiff

        它还允许对图像堆栈“深度”进行颜色编码并进行 z 投影。

        希望对你有帮助

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多