Python Forum
Unable to capture all images of a multipage TIFF file in a merge - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Unable to capture all images of a multipage TIFF file in a merge (/thread-31042.html)



Unable to capture all images of a multipage TIFF file in a merge - bendersbender - Nov-19-2020

Hello!

I'm new to Python and I'm using the code below to merge TIFF files together. In a folder, it reads an index file to determine which TIFF files should be merged together. It then merges those files by concatenating the images on top of each other into new (large) TIFF files. (i.e. File A merges with File F to create file AF, file B merges with file G to create file BG).

It works but it does not capture all the images in a multipage TIFF file; it only captures the image from the first page. In other words, in situations where I should have 4 concatenated images in one file, I only end up with two. From what I have read in forums and websites, it seems like I need to incorporate image.seek() somehow. Would anyone be able to provide guidance on this?

from PIL import Image

filename = "index"

#Open the file to read it
infile = open(filename, 'r') 

line=infile.readlines() 
s="C"      #search for string. "C" is marker for merging 2 lines.
i=0             #line counter, start a line 1


def get_concat_h(im1, im2):
    dst = Image.new('RGB', (im1.width + im2.width, im1.height))
    dst.paste(im1, (0, 0))
    dst.paste(im2, (im1.width, 0))
    return dst

def get_concat_v(im1, im2):
    dst = Image.new('RGB', (im1.width, im1.height + im2.height))
    dst.paste(im1, (0, 0))
    dst.paste(im2, (0, im1.height))
    return dst




while i<len(line):
        if line[i].find(s)!=-1:
     
                sline = line[i].split(',') 
                CheckFront= sline[8]            
                CheckFront = CheckFront.rstrip("\n")  
                #print(CheckFront)       
        
                sline2 = line[i+1].split(',') 
                List_length = len(sline2)      

                CheckInfo = sline2[8]
                CheckInfo = CheckInfo.rstrip("\n")  
                #print(CheckInfo)            
                #if List_length == 10:        
                #    CheckInfo2= sline2[9]
                #    print(CheckInfo2)
                
                im1 = Image.open(CheckFront)
                im2 = Image.open(CheckInfo)
          
                #get_concat_h(im1, im2).save(CheckFront+' ' +CheckInfo+ '.tif')     #horizonatal arrangement. Saves merged file, name is combo of files merged.
                get_concat_v(im1, im2).save(CheckFront+' ' +CheckInfo+ '.tif')    #vertical arrangement
    
        i+=1      #counter increments to go to next line