IrfanView - CLI and Batch Mode for Scanned Documents
aka "Stitching Pairs of Graphic Files"
Background information:
When you scan a book with two pages at a time, you can then use ScanTailor to clean, straighten, de-speckle, de-warp and resize all the pages in a batch file.
ScanTailor is a free open source "Postscan Processing Tool" (http://scantailor.org/downloads/)
Scantailor outputs graphic files for each page (left page and right page), and that is the problem that this post solves: reassembling the two pages in one file.
If you want to reassemble the pair of pages in one one file "exactly as the pages were scanned",
you can use IrfanView as a command line tool (CLI) and save the commands in a batch file (DOS script).
I read that you need IrfanView v.4.20 for this special use of the batch mode (I did not verify this assertion).
One place where I found v.4.20 is http://www.oldversion.com/windows/downl ... nview-4-20 (there might be other places)
-- Read Carefully the screens during installation:
-- Do not install the toolbars
-- Archive password: oldversion
Here are the two steps: Resizing the Left/Right pages and stitching the pairs of pages.
Part one: First you have to resize separately the left and right pages, which is easy because ScanTailor assigns specific names for the Left and Right pages, i.e. the file names end in _1L for left pages and _2R for right pages.
-- Here are the command lines:
C:\irfanview420\i_view32.exe C:\inputfiles\*_1L.tif /crop=(0,0,2825,4455) /aspectratio /resample /tifc=4 /convert C:\croptile\*.tif
C:\irfanview420\i_view32.exe C:\inputfiles\*_2R.tifcrop=(95,0,2940,4455) /aspectratio /resample /tifc=4 /convert C:\croptile\*.tif
Note:
= I use tiff B&W files, because this is what works well with ScanTailor.
= An instance of IrfanView420 is residing in folder: C:\irfanview420
= The input files created by ScanTailor are in folder C:\inputfiles
= C:\croptile is the folder where the resized files and the batch file will be stored (this is addressed later in this post)
= The list of parameters (example here: crop=(0,0,2825,4455) must be determined for each book/job, for this step use a suited image viewer/editor, such as MS-Paint
Part two: Reassembling the left and right pages in one file with IrfanView 4.20.
There is an excellent explanation and script at: https://community.ebay.com/t5/Archive-T ... p/16993753
Just in case this gem disappears from the web, I reproduce it here below.
Note that I tried unsuccessfully to save it on the waybackmachine but I got a HTTP 302 and a HTTP 303 error.
>>>>>>>> Partial Copy of the web page BEGINS here
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Partial Copy of the web page ENDS hereHere is the DOS batch file to stitch two photos together using IrfanView. You will have to modify the file to use it.
Edit the document in a plain text editor like Notepad, then save the file with a suffix of ".bat" which, when clicked, will open a black DOS window.
I call it:
IrfanBatchStitch.bat
Code: Select all
cls @ echo. @ echo. @ echo. _________________________________________________________ @ echo. * * @ echo. * This batch job will run IrfanView * @ echo. * to combine two photos into one. * @ echo. * * @ echo. * It must be placed in the folder containing photos * @ echo. * and will write composites to the same folder. * @ echo. * * @ echo. * To Abort, press the Ctrl+C keys. * @ echo. * or close the window with the top right X * @ echo. *_________________________________________________________* @ echo. @ echo. @ echo. @ pause @ echo OFF :: ------------------------------------ :: DOS Batch file will run IrfanView :: to composite two photos into one :: Place this DOS batch within the :: folder that contains the photos :: ------------------------------------ :: Set the full path to the irfanview program :: Works with irfanview 4.23 but not with 4.36 :: The path for the photos will be the same as this routine, :: so the routine will determine it automatically :: ------------------------------------ set progpath=C:\Program files\IrfanView\i_view32.exe set workpath=%CD%\ :: ------------------------------------ :: The orientation will be 1 for horizontal composites and 2 for vertical composites. :: The three file suffixes will indicate the two source files and the final destination file. :: The resulting image will be resized to the value given below with a quality of 90, :: and there will be no separating gap. :: Avoid trailing spaces. :: ------------------------------------ set orientation=1 set suffix1=a.jpg set suffix2=b.jpg set suffix3=c.jpg set imgsize=1600 :: ------------------------------------ :: Destinations files will be overwritten without warning. :: Images not found will be noted in the error file. :: ------------------------------------ echo.Working in folder: >imgerrors.txt echo. %workpath% >>imgerrors.txt setlocal EnableDelayedExpansion for %%i in (*%suffix1%) do ( set dir=%orientation% set orig=%%i set mate=!orig:%suffix1%=%suffix2%! set final=!orig:%suffix1%=%suffix3%! if exist !mate! ( start /wait "" "%progpath%" "/panorama=(!dir!,%workpath%!orig!,%workpath%!mate!)" /resize_long=%imgsize% /aspectratio /resample /jpgq=90 /convert="%workpath%!final!" ) else ( echo.Could not find: !mate! >>imgerrors.txt ) ) endlocal
The first field to edit is the working directory of your IrfanView program. You will find it in the root program files or under a specific user, so change the path to match your own.
set progpath=C:\Program files\IrfanView\i_view32.exe
The next fields to edit are the stitching orientation and the suffixes you are using. suffix1 and suffix2 are the two source images and suffix3 is the destination image. I use a,b,c but you can use front, back, combo or anything else that is meaningful.
set orientation=1
set suffix1=a.jpg
set suffix2=b.jpg
set suffix3=c.jpg
set imgsize=1600
You don't need to edit any other parts of the code.
To run the code, you will need to place the DOS batch program and all your photos into one folder. That means your images can not have the same names as you now have them. It also means they will be stitched together in the same orientation, so make and process a separate folder for horizontal and vertical.
Before stitching your photos, you can preprocess them in IrfanView to batch rename (with the suffixes you will need) and write the new filenames to the new folder where they will later be stitched. Since the merged photos have no separation, you may also want to expand the canvas on two sides with a couple of pixels of white background in the same batch process where you rename your photos. However, if your photos are something like trading cards on a scanner background, then you won't need separation because the scanner background will provide it.
Now here is my code adapted from the above script:
Code: Select all
set orientation=1
set suffix1=_1L.tif
set suffix2=_2R.tif
set suffix3=.tif
echo.Working in folder: > imgerrors.txt
echo. %workpath% >> imgerrors.txt
setlocal EnableDelayedExpansion
for %%i in (*%suffix1%) do (
set dir=%orientation%
set orig=%%i
set mate=!orig:%suffix1%=%suffix2%!
set final=!orig:%suffix1%=%suffix3%!
if exist !mate! (
start /wait "" "%progpath%" "/panorama=(!dir!,%workpath%!orig!,%workpath%!mate!)" /aspectratio /resample /tifc=4 /convert="%workpath%!final!"
) else (
echo.Could not find: !mate! >>imgerrors.txt
)
)
endlocal
del *L.*
del *R.*
It is stored in a file I named croptile.bat placed in folder c:\croptile (you can name it any name you like). You can place it where you want, but make the corresponding path changes.
Now that we have explained each step, let's put them together in one batch file:
>>>>>>>> Begins Here
Code: Select all
:: Part ONE
C:\irfanview420\i_view32.exe C:\inputfiles\*_1L.tif /crop=(0,0,2825,4455) /aspectratio /resample /tifc=4 /convert C:\croptile\*.tif
C:\irfanview420\i_view32.exe C:\inputfiles\*_2R.tifcrop=(95,0,2940,4455) /aspectratio /resample /tifc=4 /convert C:\croptile\*.tif
:: Part TWO
set orientation=1
set suffix1=_1L.tif
set suffix2=_2R.tif
set suffix3=.tif
echo.Working in folder: > imgerrors.txt
echo. %workpath% >> imgerrors.txt
setlocal EnableDelayedExpansion
for %%i in (*%suffix1%) do (
set dir=%orientation%
set orig=%%i
set mate=!orig:%suffix1%=%suffix2%!
set final=!orig:%suffix1%=%suffix3%!
if exist !mate! (
start /wait "" "%progpath%" "/panorama=(!dir!,%workpath%!orig!,%workpath%!mate!)" /aspectratio /resample /tifc=4 /convert="%workpath%!final!"
) else (
echo.Could not find: !mate! >>imgerrors.txt
)
)
endlocal
del *L.*
del *R.*
Just double click in Windows on the batch file to run it.