Community Pick: Many members of our community have endorsed this article.
Editor's Choice: This article has been selected by our editors as an exceptional contribution.

How To Combine-Merge-Append a Large Batch of TIFF Files

Joe WinogradDeveloper
CERTIFIED EXPERT
50+ years in computers
Development•Sales
CIO•Document Imaging
EE — FELLOW 2017
MVE 2015,2016,2018
RENOWNED 2018,2019
CERTIFIED GOLD 2020
Published:
Updated:
Edited by: Andrew Leniart
Article Update 13-March-2020: I removed the full source code and the code snippets. The article that remains should act as a "design roadmap" for members who want to write the code in the programming language of your choice. If you are interested in discussing the program further, please contact me via the EE message system. 

INTRODUCTION

The inspiration for this Article was a fascinating question here at Experts Exchange on combining TIFF files. Since it is in an area of extreme interest to me ( Document Imaging) and since the solution involves two of my all-time favorite freeware products – IrfanView for the TIFF image processing and AutoHotkey for the scripting – I decided to publish the solution as an Article, with a lot more detail put into it than a typical response to a question.

INSTALLATION INSTRUCTIONS

The original poster (OP) of the problem ( KHMaddox) said he has no programming experience at all, so I made the solution suitable for such a user. All you have to be capable of doing is download and install the two freeware products, IrfanView and AutoHotkey, and then run the script attached to this Article, as follows:

(1) Install AutoHotkey – http://ahkscript.org (also, see my EE article: AutoHotkey - Getting Started)

Click the Download button at the page above, save the install file, and run it.

(2) Install IrfanView – http://www.irfanview.com/

Click the Download button at the page above, save the install file, and run it. The script assumes that IrfanView is installed in the default location. If it isn't, you'll need to modify the script accordingly. As a side note, if you'd like to have PDF support in IrfanView, click the PlugIns link at the page above, save the install file, and run it. PDF capability is not needed for the solution in this Article, but as long as you're installing IrfanView, it's a great feature to have.

(3) Run Program – Combine-TIFF.ahk

Download the attached script called Combine-TIFF.ahk and then run it by simply double-clicking on it in Windows Explorer (or whatever file manager you use). Since its file type is AHK, AutoHotkey will be launched to process it. If you prefer, the file may be turned into an executable via the AutoHotkey compiler, which is installed during the standard installation of AutoHotkey. If you right-click on an AHK file in Windows Explorer (or whatever file manager you use), there will be a context menu pick called Compile Script:


Select that and it will create an EXE file, which is a stand-alone, no-install executable of the AHK program.

PROBLEM DESCRIPTION

The OP's problem is that a folder has many TIFF files with a specific naming convention, as follows:

1979_00032983_0001.tif
1979_00032983_0002.tif
1979_00032984_0001.tif
1979_00032984_0002.tif
1979_00032984_0003.tif
1979_00032985_0001.tif

When files have the first 13 characters the same, as in the case of the first two files above, they should be in the same file. The idea is to take those two files and create a new file with the file name being just the first 13 characters. In other words, put [1979_00032983_0001.tif] and [1979_00032983_0002.tif] (in that order) into a file named [1979_00032983.tif]. Likewise, using the example above, put [1979_00032984_0001.tif], [1979_00032984_0002.tif], and [1979_00032984_0003.tif] (in that order) into [1979_00032984.tif]. Even items with just one file in the list should be processed, so using the example above, put [1979_00032985_0001.tif] into [1979_00032985.tif].

Note that the input TIFF files may have any number of pages in them.

The OP specifically had 13 lead-in characters, but I generalized the solution so it handles any number of lead-in characters that are needed to match (the user enters the number). This number may be as small as 1, such as with these source files:

11.tif
12.tif
21.tif
22.tif
23.tif
31.tif

The above would result in the output files:

1.tif
2.tif
3.tif

Before proceeding with my solution, I want to say that there are many GUI tools out there that will do this, although I've never tried any of them. A web search for "merge tiff" or "combine tiff" will give plenty of hits, such as these:

    http://www.tiff-split-combine.com/
    http://www.pdf-tiff-tools.com/TIFF-Combiner.html

Some of them even claim to have batch capability, but since I haven't tried any of them myself, I don't know if they can really do what the OP requested, and I don't know the cost. So I decided to proceed with my home-brew solution, where the two tools needed are freeware.

HOW THE PROGRAM WORKS

For those interested in understanding how the script works, the remainder of the Article shows the entire script broken down into code snippets, with a description of what each snippet does, including screenshots where appropriate.

Code snippet:
 
SetBatchLines,-1 ; run at maximum speed
What it does: Sets the script to run at maximum speed, i.e., no "sleeping" will occur in the program.

Code snippet:
 
removed
What it does: Checks to see if IrfanView is installed. It looks in the standard location for 32-bit Windows C:\Program Files\IrfanView\i_view32.exe and the standard location for 64-bit Windows C:\Program Files (x86)\IrfanView\i_view32.exe. If it doesn't find IrfanView in either folder, it displays a message and exits.


Code snippet:
 
removed
What it does: Warns the user that existing files in the destination folder will be overwritten with no warning, and then gives the user the opportunity to exit or continue.

Code snippet:
 
removed
What it does: Initializes some variables.

Code snippet:
 
removed
What it does: Asks the user for the number of lead-in characters that need to match.


If the entry is not an integer and/or not greater than zero, it displays a message and gives the user the opportunity to try again or exit.

 


Code snippet:
 
removed
What it does: Asks the user to enter the full path of the source folder. It allows the user to navigate/browse to it or type/paste it in. It looks for an ending backslash on the path name and if one was not entered, it appends one (in other words, it works whether or not the user includes the ending backslash in the path).


It then checks to see if a source folder was entered, and if so, if the folder exists. If either is not true, it gives the user the opportunity to exit or continue. Note: whether or not the source folder can be reported as null with the Browse For Folder dialog depends on the operating system, so the program checks for it.


Code snippet:
 
removed
What it does: Asks the user to enter the full path of the destination folder. It allows the user to navigate/browse to it or type/paste it in or create it. It looks for an ending backslash on the path name and if one was not entered, it appends one (in other words, it works whether or not the user includes the ending backslash in the path).


It then checks to see if a destination folder was entered, and if so, if the folder exists, giving the user the opportunity to create it, exit, or try again to enter the name. Note: whether or not the destination folder can be reported as null with the Browse For Folder dialog depends on the operating system, so the program checks for it.



Code snippet:
 
removed
What it does: Asks the user to confirm that the chosen source and destination folders are correct, providing the option at this point to continue or exit.


Code snippet:
 
removed
What it does: Asks the user to enter compression method and checks that it is correctly specified. It keeps trying until the user enters a valid number or decides to exit.





Code snippet:
 
removed
What it does: Initializes variables that are used to track operational statistics, which will be reported in Operation Complete dialog.

Code snippet:
 
removed
What it does: Loops through all of the files in the source folder, sorted in file name order (ascending), ignoring any non-TIFF files. It counts the number of TIFF files and the number of non-TIFF files, and it accumulates the total size (in bytes) of the TIFF files. It displays a dialog box with a green progress bar that moves to the right during processing, also showing the name of the file currently being processed.

Code snippet:
 
removed
What it does: Checks to see if the current file in the list has the same lead-in characters as the previous file. If it doesn't, then it represents a new output file, and the program copies the current file in the source folder to a new file in the destination folder, using a file name composed of just the lead-in characters. If the copy fails, it displays the error code and all of the relevant parameters to assist in troubleshooting the problem. Also, it counts the number of TIFF files created and accumulates the total size (in bytes) of the TIFF files created.

Code snippet:
 
removed
What it does: The current file has the same lead-in characters as the previous file, so it appends the current file to the combined file in the destination folder. To do this, it (i) runs IrfanView with the "/multitif" parameter to create a temporary file; (ii) deletes the existing combined file in the destination folder; and (iii) renames the temporary file to have the same name as the just-deleted combined file. If the IrfanView call or the file deletion or the file renaming fails, it displays the error code and all of the relevant parameters to assist in troubleshooting the problem.

Code snippet:
 
removed
What it does: Finalizes and formats all of the statistics from the operation and displays them in an Operation Completed dialog box.


It also asks if the user wants to save the statistics in a text file. If the user says Yes, it creates a file with the name Operational_Statistics_YYYY-MM-DD_HH.MM.SS.txt in the destination folder (where YYYY-MM-DD_HH.MM.SS are the ending date and time of the run).


The text file looks like this:

Beginning date and time: 2013-03-18_21.06.15
Compression method used: ITU-T Group 4
Number of source files processed: 23
Size of source files (bytes): 324,806
Number of destination files created: 6
Size of destination files (bytes): 324,670
Number of non-TIFF files ignored: 3
Ending date and time: 2013-03-18_21.06.17
Elapsed time (minutes:seconds): 0:2

I hope this helps the OP as well as other EE members. Although I did a bit of generalization, I realize that the solution is still rather specific to the OP's requirements. However, by providing the source code, I'm sure that other folks with similar needs will be able to modify the program to suit their purposes.

If you find this article to be helpful, please click the thumbs-up icon below. This lets me know what is valuable for EE members and provides direction for future articles. Thanks very much! Regards, Joe
12
12,386 Views
Joe WinogradDeveloper
CERTIFIED EXPERT
50+ years in computers
Development•Sales
CIO•Document Imaging
EE — FELLOW 2017
MVE 2015,2016,2018
RENOWNED 2018,2019
CERTIFIED GOLD 2020

Comments (46)

Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Author

Commented:
Hi Deacon,
You're very welcome, and my thanks to you for the compliments — I really appreciate hearing them! I'm glad to know that MergeTIFF worked well for you and was a resounding success — music to my ears! Regards, Joe
Nathan EmchNetwork Analyst

Commented:
Your script sounds like exactly what we need, but I do not see it available for download anywhere on this page.  Am I missing something, or did your "Combine-TIFF.ahk" script get pulled from this page?
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Author

Commented:
Hi Nathan,
Yes, I pulled the script from the article. My initial intention was to re-post the code, but I have decided not to do that. Instead, I am going to rewrite this article, and a similar article on merging PDF files, as "roadmap design" articles, with enough information to help folks write their own programs/scripts. However, I have enhanced the program over the years into what I now call MergeTIFF™. As you can see in the comments above from May of this year, Kit Maddox and Deacon Aspinwall had great results with MergeTIFF. But, as also mentioned above, I'm not yet ready to expose the program publicly on the Internet, so I'll write you a PM in the EE Message System to discuss this further, as I did with both Kit and Deacon. Regards, Joe
Brandon GSenior Developer

Commented:
Hello Joe,
This sounds like a very magical solution and exactly the type of resource needed to process the nearly 1Million individual tif image files that I have, which are stored similarly as your article describes i.e. "tdr-2772-134446_page_1.tif","tdr-2772-134446_page_2.tif","tdr-2772-134446_page_3.tif", and "tdr-2772-134446_page_4.tif". All the files are in a single location and I am looking to merge the multiple pages into single multi-page tif, just as you describe.

Anyway you can be of assistance or I can be a part of the Beta testing?

Any assistance is much appreciated. Thank you in advance.
Brandon
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Author

Commented:
Hi Brandon,
Thanks for joining Experts Exchange today, reading my article, and endorsing it. You're right — MergeTIFF does exactly what you want. For example, by specifying 15 as the number of first characters that need to match, it will merge these files...

tdr-2772-134446_page_1.tif
tdr-2772-134446_page_2.tif
tdr-2772-134446_page_3.tif
tdr-2772-134446_page_4.tif

...into this file:

tdr-2772-134446.tif

I'll write you a PM in the EE Message System to discuss this further, as I've done with many EE members for this program and several other programs. I should point out that this is now an acceptable method, compliant with EE's Terms of Use, since EE removed the Gigs product and the Hire Me feature. Regards, Joe

View More

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community