How would you like to turn a photo like this one:
Into a photo like this one:
AUTOMATICALLY??? (and in batch-loads at a time!!!)
In my previous post, I introduced the art and science of book scanning, and announced that I am going digital... painfully digital. In this post, I will present the reader with a simple, yet powerful batch script that (when setup as forthwith described) will clear up any lighting issues one may encounter when attempting to digitize that lovely collection of Shakespearean sonnets! Hooray! :)
Let's hope this is not much ado about nothing, Mr. W.H., eh? eh?
Step One: Create folders and files!
Create a new folder anywhere you like, and name in anything you wish. I called my folder ePress (and will subsequently refer to this folder as the root folder for this project).
Now, create a file within this root folder and name it: "retinex.bat" (sans quotes). Leave the file empty for now...
Next, create a folder within the root folder, and name it "bin" (sans quotes).
Within the "bin" folder, create another folder and name it "img" (you know by now, right?)
One last time (I swear) create a folder within the "img" folder, and name it "magick" (sic, and sans quotes).
Finally... you should now have the following structure:
Step Two: Download ImageMagick Binaries (*.exe files)
Visit http://www.imagemagick.org and find the binary release for your operating system. Now, DO NOT download the installer... You will need the release that comes as a *.zip file, as it contains all of the individual executables that comprise the ImageMagick (TM) tool-set.
At the time of the post, the following was the direct link to the zip file: DOWNLOAD.
Once you have the zip file (sometimes called the "portable" version), extract all of the executables (and whatever other junk: www, etc...) to the "magick" folder you created in step one. All we are really concerned about here is obtaining the "convert.exe" program!
Step Three: Create some more folders...
Within the root folder (ePress, for me...) create a folder and name it "tmp" (sans quotes).
Next, within the "tmp" folder, create a folder and name it "retinex" (you get the idea)...
Go BACK to the ROOT folder now...
Within the root folder, create a folder and name it "output".
Within the "output" folder, create a folder and name it "retinex".
Phew! You should now have the following new structures:
Step Four: Paste batch code into "retinex.bat" file!
First off, let me say that this code IS NOT an original concept! In fact, I only adapted one of the many scripts available to perform retinex image processing... this particular script was a shell script from Fred's ImageMagick Scripts
Now, on with the show...
Paste the following code into the file (that you created earlier) called "retinex.bat":
echo -- RETINEX (R) IMAGE PROCESSING --
echo SCRIPT BY: Sean A. Finucane
echo July 22, 2011
echo Adapted from:
echo Fred's ImageMagick Scripts,
echo Retinex Bash Script
REM RETINEX IMAGE PROCESSING (HSL)
REM PATH TO OUTPUT FILE
FOR %%I IN (echo %1) DO SET outputname=%%~nI
REM PATH TO IMAGE MAGICK CONVERT.EXE
REM LOGARITHM TO USE FOR RETINEX ALGORITHM
echo Output file will be: %outfile%
echo REPAGING... Please wait.
%convert% -quiet -regard-warnings %1 +repage %tmpA%
REM HSL COLOR MODE PROCESSING
REM Save original
echo HSL COLOR MODE PROCESSING... Please wait.
%convert% %tmpA% %tmp0%
REM Convert to HSL reusing $tmpA for Lightness
echo Convert to HSL... Please wait.
%convert% %tmpA% -colorspace HSL -channel R -separate %tmpH%
%convert% %tmpA% -colorspace HSL -channel G -separate %tmpS%
%convert% %tmpA% -colorspace HSL -channel B -separate %tmpA%
REM PROCESS IMAGE
echo Processing Reflectance Images - Please Wait, this may take some time...
REM Get three resolution sizes (in this case, default=5, 20, and 240
REM Create reflectance images at each resolution size
echo STAGE 1:
%convert% %tmpA% ( +clone -blur 0x%res1% ) -fx %formula% %tmp1%
echo STAGE 2:
%convert% %tmpA% ( +clone -blur 0x%res2% ) -fx %formula% %tmp2%
echo STAGE 3:
%convert% %tmpA% ( +clone -blur 0x%res3% ) -fx %formula% %tmp3%
echo Finalizing... Please wait.
REM Average results (normalize as log reduces range of values)
%convert% %tmp1% %tmp2% %tmp3% -evaluate-sequence mean -normalize %tmp1%
REM Convert back to RGB and OUTPUT!
%convert% %tmp0% -colorspace HSL %tmpH% -compose CopyRed -composite %tmpS% -compose CopyGreen -composite %tmp1% -compose CopyBlue -composite -colorspace RGB %outfile%
... and VOILA! You may now drag and drop any SINGLE image file into the "retinex.bat" file, and it will be subsequently processed. As I mentioned previously, the primary advantage of retinex processing is the ability to improve a WIDE variety of images AUTOMATICALLY (i.e. without the need to tweak settings). The primary disadvantage is that the process is really quite S.L.O.W.
But, if you create a file (name it whatever you wish, as long as it is in the same directory as "retinex.bat") and paste the following batch script code into it:
IF [%1]== GOTO FINISH
REM OTHERWISE, WE SHOULD PERFORM THE DESIRED OPERATION
CALL "%curDirectory%%batchProg%" "%%~dp1%%~n1%%~x1"
REM SHIFT THE ARGUMENT VARIABLES (%1 = %2, etc...)
... you will then be able to drag and drop whole groups of files into the above batch file AT ONE TIME! Hooray for batch processing! :)
My file was cryptically named "batch_retinex.bat"...