Share multi-page tiff with python

What is the best way to split multi-page TIFF in python? PIL does not seem to support multi-page images, and I did not find the exact port for libtiff for python. Will PyLibTiff be the way to go? Can someone provide a simple example of how I can parse multiple pages in TIFF?

+6
source share
6 answers

I use ImageMagick as an external program to convert multi-page fax to visible 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 

converts the first page to PNG

aaa.tiff [1] will be the second page, etc.

Or, to extract all images, follow these steps:

 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 

So, just to split one multi-page TIFF into multi-page TIFF, you have to do:

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

and then work with temporary files: / tmp / out-12345 - *. tiff

However, ImageMagick can do a lot of processing, so you can probably achieve the desired result in one command.

+3
source

The project (disclosure: I am one of the main authors, this question was one of the questions that prompted me to work on it), which facilitates this: PIMS . The PIMS core is essentially a cleaned and generalized version of the following class.

Class for extracting the base frame + simple iteration.

 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) 
+7
source

ImageMagick worked very well for me. If you split the TIFF file, mainly convert from TIFF to TIFF, you can use the flag to force the output files to be saved as separate TIFF files. To do this, try

 convert input.tif output-%d.tif 

The% d statement is a C-Printf% d style. So, if you need a sequence of 3 fields, you can say

 convert input.tif output-%3d.tif 

etc.% d is replaced by the "scene" number of the image. Now, scene numbers may or may not always begin with 0 (or 1 if you want to). To set the sequence the way you want, try

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

This will start the sequence right from the amount you specified.

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

The magic is really !! :)

This documentation link contains more detailed information. This also works on my windows machine.

+5
source

Next, a tif file with several frames is split into tif files, where each file is a single frame.

 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 
+1
source

After installing ImageMagick on Windows. Try the following below on the command line

1. Verify ImageMagick TIF File Conversion

 magick convert input.tif out.tif 

2. Convert one multi-page TIF file to multiple files

 magick convert -verbose input.tif otput.png 
0
source

You can convert it to PDF and use pyPDF to split pages

-2
source

Source: https://habr.com/ru/post/910303/


All Articles