View Single Post
Old 03-19-2008, 09:38 AM   #14
axel77
Fanatic
axel77 has learned how to read e-booksaxel77 has learned how to read e-booksaxel77 has learned how to read e-booksaxel77 has learned how to read e-booksaxel77 has learned how to read e-booksaxel77 has learned how to read e-booksaxel77 has learned how to read e-booksaxel77 has learned how to read e-books
 
Posts: 584
Karma: 914
Join Date: Mar 2008
Device: iliad
If anybody interested, I optimized my script. Now some computation time is safed by doing processor intensive tasks only once with intermediate file, and the resulting pdf file has now exactly the size of the images:

This time I converted a book, I scanned in a year ago, that time gray scaled, required a little different convertion parameters to look nice again, but the end result is aprox. the same, so for my experience it doesn't matter if the original scan is gray or monochrome.

Note you can watch the pages being build up by saying "gthumb tmp" in another shell in the same directory, so you don't have to run through the whole script to see if the pages will look like what you want or if they don't.

Oh Yes the script doesn't clean up, you'll have to do by hand (Honestly, I don't want it to, since often enough I only want to redo certain steps, commenting the others out)

Code:
#!/bin/bash 
# parameter: tif files are prefixed with this:
NAME=turkle_
# parameter: crop areas (WIDTHxHEIGHT+OffsetLeft+OffsetTop)
CROPLEFT=750x1100+200+0
CROPRIGHT=750x1100+1000+0
# this embosses text for B/W scans, so it looks nice when resized on the iliad:
#CONVERTOPTS="-trim -blur 1 -threshold 65534"
# this embosses text for Grayscans, so it looks nice when resized on the iliad:
CONVERTOPTS="-trim -threshold 45000 -gaussian 1x0.3 -threshold 55534"

shopt -s extglob
mkdir tmp

#determine number of pages there..
MAXPAGE=1;
while test -f $NAME*(0)$MAXPAGE.TIF; do let MAXPAGE++; done
let MAXPAGE--

echo "converting $MAXPAGE pages, this will take a while!"

echo "STEP 1 OF 3: preparing pages"
for ((i = 1; $i <= $MAXPAGE; i++)) do
   echo "@$i of $MAXPAGE";
   convert $CONVERTOPTS $NAME*(0)$i.TIF tmp/tmp.TIF; 
   convert -crop $CROPLEFT tmp/tmp.TIF tmp/p$((($i-1)*2)).TIF; 
   convert -crop $CROPRIGHT tmp/tmp.TIF tmp/p$((($i-1)*2+1)).TIF;
   ALL="$ALL tmp/p$((($i-1)*2)).TIF tmp/p$((($i-1)*2+1)).TIF"; 
done

echo "STEP 2 OF 3: adjoining pages"
echo ALL=$ALL
tiff2ps $ALL > output.ps

echo "STEP 3 OF 3: converting/compressing to PDF"
DW=$(grep "^%%BoundingBox: " output.ps | cut -d' ' -f 4 | sed -e "s/[^0123456789]//g")
DH=$(grep "^%%BoundingBox: " output.ps | cut -d' ' -f 5 | sed -e "s/[^0123456789]//g")
ps2pdf -dDEVICEWIDTHPOINTS=$DW -dDEVICEHEIGHTPOINTS=$DH output.ps
echo "done!"
axel77 is offline   Reply With Quote