<

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

Published on
29,929 Points
11,229 Views
12 Endorsements
Last Modified:
Awarded
Joe Winograd
50+ years in computer industry •Everything from development to sales •CIO •Windows •Document Imaging •EE MVE 2015,2016,2018 •EE FELLOW 2017
Update 21-May-2015: I temporarily removed the source code and the code snippets to make major changes to the program. Regards, Joe

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:

AutoHotkey 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

Open in new window

What it does: Sets the script to run at maximum speed, i.e., no "sleeping" will occur in the program.

Code snippet:
 
temporarily removed

Open in new window

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.

IrfanView not found
Code snippet:
 
temporarily removed

Open in new window

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.

Existing files overwritten
Code snippet:
 
temporarily removed

Open in new window

What it does: Initializes some variables.

Code snippet:
 
temporarily removed

Open in new window

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

Enter number characters 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.

NumFirstChars must be integerNumFirstChars must be at least 1
Code snippet:
 
temporarily removed

Open in new window

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).

Navigate to or type-paste Source folder
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.

Source folder must be specifiedSource folder does not exist
Code snippet:
 
temporarily removed

Open in new window

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).

Navigate to or type-paste or create Destination folder
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.

Destination folder must be specifiedDestination folder does not exist
Code snippet:
 
temporarily removed

Open in new window

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.

Confirm folders
Code snippet:
 
temporarily removed

Open in new window

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.

Enter compression methodCompression must be specifiedCompression must be integerCompression must be 0-7
Code snippet:
 
temporarily removed

Open in new window

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

Code snippet:
 
temporarily removed

Open in new window

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.

Progress Bar
Code snippet:
 
temporarily removed

Open in new window

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:
 
temporarily removed

Open in new window

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:
 
temporarily removed

Open in new window

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

Operation Completed
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).

Operational Statistics saved
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
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free