use*_*643 8 python tiff python-imaging-library
使用python拆分多页TIFF的最佳方法是什么?PIL似乎不支持多页图像,我没有找到python的libtiff的确切端口.PyLibTiff会走的路吗?有人可以提供一个简单的例子,说明我如何解析TIFF中的多个页面?
一个项目(披露:我是主要作者之一,这个问题是促使我研究它的事情之一)这使得这很容易就是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)
Run Code Online (Sandbox Code Playgroud)
Imagemagick为我工作得非常好。如果要分割一个tiff文件,基本上是从tiff转换为tiff,则可以使用一个标志来强制将输出文件保存到单个tiff文件中。为此,请尝试
convert input.tif output-%d.tif
Run Code Online (Sandbox Code Playgroud)
%d运算符是C-Printf样式%d。因此,如果您需要3个字段的运行序列,可以说
convert input.tif output-%3d.tif
Run Code Online (Sandbox Code Playgroud)
依此类推。%d被替换为图像的“场景”编号。现在,场景编号可能会或可能不会始终以0(或1,如果您这样想)开头。要以所需方式设置序列,请尝试
convert input.tif -scene 1 output-%3d.tif
Run Code Online (Sandbox Code Playgroud)
这将从您提供的计数开始该序列。
convert -scene 1 input.TIF output-%d.TIF
output-1.TIF
output-2.TIF
output-3.TIF
Run Code Online (Sandbox Code Playgroud)
魔术师的确如此!:)
此文档链接具有更多详细信息。这也适用于我的Windows计算机。
我使用 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
Run Code Online (Sandbox Code Playgroud)
是否将第一页转换为 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
Run Code Online (Sandbox Code Playgroud)
因此,要将一个多页 TIFF 拆分为多页 TIFF,您必须执行:
convert in-12345.tiff /tmp/out-12345.tiff
Run Code Online (Sandbox Code Playgroud)
然后使用临时文件:/tmp/out-12345-*.tiff
然而 ImageMagick 可以进行大量处理,因此您可能可以通过一个命令达到您想要的结果。