This tutorial assumes the use of Linux. I hope that Windows and Mac people can figure out how to use this approach with your own scripts.
Software Tools:
ufraw
ufraw-batch
gimp
imagemagick
ppmunwarp (http://diybookscanner.org/forum/viewtop ... =19&t=2589)
Meatspace Tools
Gray card (I use an 18%, which has rgb values of 128 128 128)
Calibration image for ppmunwarp -- see its thread. In my post in that thread, I have included eps files to use to print said image. As this calibration image also is used as a rough crop, it is handy to cut a variety of sizes for differently-proportioned books.
Steps to do before running the script
1. When photographing the book in RAW, take a photo of the calibration image and the gray card with each camera.
2. Copy images from your cameras into left and right directories.
3. Rename the gray card image as "color.ext" (ext being whatever extension your RAW photos are in. On my canons, it is CRW)
4. Rename the calibration image as "calibration.ext"
5. Rename your other files as appropriate. (I use numbers with an _L or _R ending)
Running the script...
1. Run the script in the directory containing the images
Fixing Color:
1. The script opens the color file in UFRaw and asks your for three values.
2. In UFRaw, draw a selection box over much of your gray card, and click the eyedropper button, which creates a true gray. 3. Using the exposure adjust tool, adjust until the spot values reach their true value. (In the case of 18%, it's 128, 128, 128) 4. Enter the resulting temperature, green, and exposure values into the script. 5. Now, your raw input is color adjusted and converted to the ppm format using ufraw-batch
Fixing Distortions
1. The script uses the file calibration.ppm to prepare for unwarping
2. The script now runs ppmunwarp on all images in the directory
Preparing Images for Scantailor
1. The script opens the corrected calibration image in GIMP.
2. Using the measuring tool (shift-M), determine the pixels per centimeter. (each grid point is half a centimeter) 3. Enter that number into the script
4. The script, using that value, now converts the image format into lzw compressed tif files with correct DPI information.
Cleanup
1. The script deletes the intermediate ppm and png files.
2. The script moves the final output down a directory
Run the script again on the other directory...
You are ready for Scantailor!
Things for you to adjust as needed in the script:
1. Input format for RAW files. Currently, it is entered as CRW. Change to whatever file format is appropriate.
2. Path to ppmunwarp. I installed mine in /usr/bin, so I can just call it with ppmunwarp. You may need to include the path to the binary
The Script:
Code: Select all
#!/bin/bash
#This script takes RAW files and fixes color and geometry and prepares images for scantailor.
#It requires the use of a gray card and the ppmunwarp grid.
#Change input file formats in the script as appropriate for you camera.
#Before running the script, rename the gray card as color and the calibration image as calibration
#Name all other files as you choose
##Color Calibration from a gray card.
#Using UFRaw: 1. select an area on your gray card,
#2. click the eyedropper, which equalizes RGB values
#3. adjust the exposure control to get RGB values to their ultimate goal.
#(e.g. an 18% gray card is 128 128 128)
#The script will ask for the resulting values from the gui and apply them to all images.
echo "The color calibration image is being loaded in UFRaw. Enter the following values for color correction"
ufraw color.CRW &
echo "Color temperature?: "
read temperature
echo "Green Value?: "
read green
echo "Exposure change?: "
read exposure
echo "Running Ufraw-batch, this will take a few minutes..."
ufraw-batch --temperature=$temperature --green=$green --exposure=$exposure --out-type=ppm *.CRW
##Using ppmunwarp and the calibration image, lens and perspective distortion are fixed.
echo "Calibrating geometry..."
ppmunwarp -m check.ppm calibration.ppm > calibration.bin
echo "Correcting geometry. This will take some time..."
for i in *.ppm; do
if [ -e "$i" ]; then
file=`basename "$i" .ppm`
~/software/ppmunwarp/ppmunwarp -d calibration.bin "$i" > "$file.png"
fi
done
##Using gimp, determine the pixels per centimeter
echo "Use the measuring tool in GIMP (shift-m) to determine the number of pixels per centimeter (two dots)."
gimp calibration.png &
echo "PixelsPerCM?: "
read ppc
ppi="$(echo "$ppc*2.54" | bc)"
##Imagemagick coverts images to a scantailor-compliant file
echo "ImageMagick will now convert the format into one useable by Scantailor"
mogrify -verbose -format tif -density $ppi -units PixelsPerInch -compress lzw *.png
##Housekeeping -- comment out what you don't want to happen.
##Clean temporary files
rm *.png
rm *.ppm
##Move files down a directory, in cases of Left/Right folders
mv *.tif ..
Geometry
Calibration Image -- I tried to get some keystoning and some good lens distortion in this image. I normally zoom more than that, but this is to show how it works. Geometry data from calibration image visualized Calibration image fixed!
Sample page Color Fixed Distortions fixed
Photos taken with two canon sx130 cameras, with CHDK.
Now that I've got that figured out, I guess I can't ignore my terrible lighting anymore....
The image of the book comes from Richard Stallman's excellent essay collection Free Software, Free Society. As I understand the licensing terms, I am supposed to offer the complete work to you. If you want the final djvu file of this book, let me know. It's kind of unnecessary though, since you can get the source files from which the book was printed on the FSF website. (http://www.gnu.org/philosophy/fsfs/rms-essays.pdf) I strongly suspect that the FSF would consider this a fair use case too, making the offer doubly-unnecessary.
Let me know if you have ideas to make this better! This was my first bash script, so I really had no clue what I was doing. The internet is an amazing resource to find answers to questions. (The only tricky bit was to figure out how to do floating point math, which, rather hilariously, isn't possible in bash)
Happy bookscanning!