Python Forum
Unable to capture all images of a multipage TIFF file in a merge
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to capture all images of a multipage TIFF file in a merge
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  OCR-Python from Multi TIFF to HOCR getting only Data from 1st Page of multiple TIFF JOE 0 2,120 Feb-18-2022, 03:18 PM
Last Post: JOE
  how to extract tiff images from the subfolder into. hocr format in another similar su JOE 0 1,131 Feb-16-2022, 06:28 PM
Last Post: JOE
  Creating file with images BobSmoss 1 1,351 Jan-08-2022, 08:46 PM
Last Post: snippsat
Question How can I merge several images via loop using the information of a dataframe? noahverner1995 1 1,399 Dec-31-2021, 05:03 AM
Last Post: noahverner1995
  Compressed multi page tiff wvanoeveren 2 2,613 Dec-28-2021, 11:40 AM
Last Post: Gribouillis
  Unable to upload file using FTP korenron 2 2,422 Dec-23-2021, 12:44 PM
Last Post: korenron
  How to open/load image .tiff files > 2 GB ? hobbyist 1 2,385 Aug-19-2021, 12:50 AM
Last Post: Larz60+
  error merge csv file ainisyarifaah 2 2,394 Aug-05-2021, 02:23 AM
Last Post: ainisyarifaah
  Problems with inserting images into an Excel File FightingFarmer 2 3,362 May-12-2021, 10:03 PM
Last Post: FightingFarmer
  How to merge all the files in a directory in to one file sutra 3 2,595 Dec-10-2020, 12:09 AM
Last Post: sutra

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020