Nov-19-2020, 03:09 PM
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
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?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
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 |