2012-03-09 96 views
6

用Python分割多頁TIFF的最佳方式是什麼? PIL似乎不支持多頁圖像,而且我還沒有找到python的libtiff的確切端口。 PyLibTiff會走嗎?有人可以提供一個簡單的例子,說明如何解析TIFF中的多個頁面?蟒蛇分割多頁TIFF

+0

PIL具有「多圖像」文件有限的支持 - 這塔卡納t個最低LAOD和真皮休閒您應對GIF動畫,單個幀 - 我不知道,如果TIFF加載插件允許你閱讀每一頁,但。你能發佈一個鏈接到你的tiffs嗎? – jsbueno 2012-03-09 03:51:06

+0

不幸的是...我現在處理的材料很敏感,我不知道如何從頭開始生成通用的多頁TIFF。我嘗試導出gimp中的多個圖層無濟於事。你有任何代碼示例?我可以在我的機器上試用它們。 – user1145643 2012-03-09 13:37:38

回答

2

我使用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可以做很多處理,所以你可能可以在一個命令中實現你想要的結果。

-1

你可以將其轉換爲PDF格式,並使用pyPDF分裂

頁面
+1

我寧願不轉換爲PDF格式,拆分並轉換回TIFF格式,除非絕對沒有其他方法可以拆分多頁TIFF。這似乎是一個提取單個頁面的倒退方式,但如果我錯了,請隨時糾正我。 – user1145643 2012-03-09 01:36:05

6

一個項目(披露:我是主要作者之一,這個問題是促使我去處理它的事情之一),這使得這很容易,是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) 
+0

numpy重塑的目的是什麼? – speedplane 2016-02-12 21:01:06

+0

@speedplane由於'getdata'返回一個扁平的圖像迭代器http://effbot.org/imagingbook/image.htm#tag-Image.Image.getdata – tacaswell 2016-02-13 16:18:09

+0

@tacaswell有沒有辦法將提取的幀轉換爲PNG或JPEG格式'get_frame()'完成後的文件? – comproch 2017-07-24 00:12:42

4

Imagemagick對我真的很好。如果要分割一個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機器。

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