用python拆分多页tiff

时间:2012-03-09 01:19:09

标签: python python-imaging-library tiff

使用python拆分多页TIFF的最佳方法是什么? PIL似乎不支持多页图像,我没有找到python的libtiff的确切端口。 PyLibTiff会走的路吗?有人可以提供一个简单的例子,说明我如何解析TIFF中的多个页面?

6 个答案:

答案 0 :(得分:8)

一个项目(披露:我是主要作者之一,这个问题是促使我研究它的事情之一)这使得这很容易PIMS。 PIMS的核心本质上是以下类的清理和通用版本。

进行基本帧提取+简单迭代的类。

import PIL.Image
class Stack_wrapper(object):
    def __init__(self,fname):
        '''fname is the full path '''
        self.im  = PIL.Image.open(fname)

        self.im.seek(0)
        # get image dimensions from the meta data the order is flipped
        # due to row major v col major ordering in tiffs and numpy
        self.im_sz = [self.im.tag[0x101][0],
                      self.im.tag[0x100][0]]
        self.cur = self.im.tell()

    def get_frame(self,j):
        '''Extracts the jth frame from the image sequence.
        if the frame does not exist return None'''
        try:
            self.im.seek(j)
        except EOFError:
            return None

        self.cur = self.im.tell()
        return np.reshape(self.im.getdata(),self.im_sz)
    def __iter__(self):
        self.im.seek(0)
        self.old = self.cur
        self.cur = self.im.tell()
        return self

    def next(self):
        try:
            self.im.seek(self.cur)
            self.cur = self.im.tell()+1
        except EOFError:
            self.im.seek(self.old)
            self.cur = self.im.tell()
            raise StopIteration
        return np.reshape(self.im.getdata(),self.im_sz)

答案 1 :(得分:5)

Imagemagick为我工作真的很棒。 Wnen分割tiff文件,基本上从tiff转换为tiff,可以使用标志强制将输出文件保存到单独的tiff文件。为此,请尝试

convert input.tif output-%d.tif

%d运算符是C-Printf样式%d。因此,如果您需要3场运行序列,您可以说

convert input.tif output-%3d.tif

等等。%d被图像的“场景”编号替换。现在,场景编号可能也可能不总是从0开始(如果你想那样,则为1)。要按照您想要的方式设置序列,请尝试

convert input.tif -scene 1 output-%3d.tif

这将从您提供的计数开始顺序。

convert -scene 1 input.TIF output-%d.TIF
output-1.TIF
output-2.TIF
output-3.TIF

Magick确实!! :)

文档中的

This link有更多详细信息。这也适用于我的Windows机器。

答案 2 :(得分:4)

我使用ImageMagick作为外部程序将多页传真转换为可查看的PNG:

/usr/bin/convert /var/voip/fax/out/2012/04/fax_out_L1_17.tiff[0] -scale 50x100% -depth 16 /tmp/fax_images/fax_out_L1_17-0-m.png

会将首页转换为PNG

aaa.tiff [1]将是第二页,依此类推。

或者要提取所有图像,请执行:

convert -verbose fax_in_L1-1333564876.469.tiff a.png
fax_in_L1-1333564876.469.tiff[0] TIFF 1728x1078 1728x1078+0+0 1-bit Bilevel DirectClass 109KiB 0.030u 0:00.030
fax_in_L1-1333564876.469.tiff[1] TIFF 1728x1078 1728x1078+0+0 1-bit Bilevel DirectClass 109KiB 0.020u 0:00.010
fax_in_L1-1333564876.469.tiff[2] TIFF 1728x1078 1728x1078+0+0 1-bit Bilevel DirectClass 109KiB 0.020u 0:00.010
fax_in_L1-1333564876.469.tiff=>a-0.png[0] TIFF 1728x1078 1728x1078+0+0 1-bit Bilevel DirectClass 12KiB 0.030u 0:00.019
fax_in_L1-1333564876.469.tiff=>a-1.png[1] TIFF 1728x1078 1728x1078+0+0 1-bit Bilevel DirectClass 8KiB 0.040u 0:00.039
fax_in_L1-1333564876.469.tiff=>a-2.png[2] TIFF 1728x1078 1728x1078+0+0 1-bit Bilevel DirectClass 32KiB 0.070u 0:00.070

因此,要将一个多页TIFF拆分为多页TIFF,您必须执行:

convert in-12345.tiff /tmp/out-12345.tiff

然后使用临时文件:/ tmp / out-12345 - * .tiff

然而,ImageMagick可以进行大量处理,因此您可以在一个命令中实现所需的结果。

答案 3 :(得分:1)

以下将具有多个帧的tif文件拆分为tif文件,其中每个文件是一帧。

def parse_tif(filePath):
    img = Image.open(filePath)
    for i in range (numFramesPerTif):
        try:
            img.seek(i)
            img.save('Block_%s.tif'%(i,))
        except EOFError: #end of file error

答案 4 :(得分:0)

在Windows中安装ImageMagick之后。在命令提示符下尝试以下操作

1。测试ImageMagick tif文件转换

magick convert input.tif out.tif

2。将一个多页tif文件转换为多个文件

magick convert -verbose input.tif otput.png

答案 5 :(得分:-2)

您可以将其转换为PDF并使用pyPDF分割页面

相关问题