1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
#!/bin/bash
#
# cleanup_scans.sh
#
# Bash script to deskew and deborder scanned images
#
# Copyright (C) 2008, 2015 Moreno Marzolla
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Version history:
#
# Version 0.1, 2008/08/03 - initial version
# Version 0.2, 2015/04/11 - changed /bin/sh to /bin/bash
#
# Author: moreno.marzolla (at) unibo.it
# http://www.moreno.marzolla.name/
#
# This script is used to process a bunch of color, grayscale or B/W scans
# Syntax:
#
# cleanup_scans.sh <file1> <file2> ...
#
# This will process file1, file2... and produce file1.tiff file2.tiff...
# in the current directory.
#
# Color scans will be converted into color TIFFs; Grayscale or black/white
# scans will be converted into B/W tiffs (unless the --forcecolor option
# is used). Both color and grayscale pages are automatically deskewed.
#
# At the moment this script contains some hardcoded defaults, which need
# to be removed before using it as a general-purpose scan-to-tiff processor
#
# This script requires the following external applications:
# - ImageMagick (convert and mogrify)
# - netpbm, the current development version from svn (it uses the pamtilt
# utility which is not included into the stable netpbm distribution)
# - perl (for evaluating a simple expression; I was unable to do that
# using only bash)
#
convert=convert
pamtilt=pamtilt
crop_geometry=2400x3250+0+0
deskew=yes
forcecolor=no
deborder=yes
function process_color_image {
# Adjust color levels and crop
local base=`basename $1 .image`
local skew=0
local deborder_cmd=(-fuzz 10% -fill white -draw "color 2540,3500 floodfill" )
echo -n "Processing COLOR image $1..."
if [ $deborder == "no" ]; then
deborder_cmd=();
fi
if [ $deskew == "yes" ]; then
echo -n "Tilt"
skew=`$convert -quiet $1 \
-crop $crop_geometry \
+repage \
-level 10%,80%,1 \
-monochrome \
"pnm:-" | $pamtilt`
skew=`perl -e "if (abs($skew)>0.1 && abs($skew)<3.0) { print (- $skew) } else { print 0 }"`
echo -n "(${skew})..."
fi
echo -n "converting..."
convert $1 \
-background white \
-rotate $skew \
+matte "${deborder_cmd[@]}" \
-level 10%,80%,1 \
-crop $crop_geometry \
+repage \
+matte \
-format tiff \
$base.tiff
\rm -f $base.pnm
echo "done"
}
function process_gray_image {
local base=`basename $1 .image`
local skew=0
local deborder_cmd=(-stroke black -fill black -draw "rectangle 0,0 50,3505" -draw "rectangle 0,0 2548,50" -draw "rectangle 0,3405 2548,3505" -draw "rectangle 2448,0 2548,3505" -fill white -draw "color 0,0 floodfill")
if [ $deborder == "no" ]; then
deborder_cmd=();
fi
echo -n "Processing GRAY image $1..."
# If the image is already black and white, then simpler operations must be performed
local bitssample=`tiffinfo $1 | grep Bits | tr -c -d [:digit:]`
if [ $bitssample -ne "1" ]; then
echo -n "B/W (slow)..."
$convert -quiet $1 \
-colorspace gray \
-level 10%,90%,1 \
-blur 2 \
+dither \
-monochrome \
-flatten "${deborder_cmd[@]}" \
-crop $crop_geometry \
+repage \
\( -write $base.pnm \) \
+matte $base.tiff
else
echo -n "B/W (fast)..."
$convert -quiet $1 "${deborder_cmd[@]}" \
-crop $crop_geometry \
+repage \
\( -write $base.pnm \) \
+matte $base.tiff
fi
if [ $deskew == "yes" ]; then
echo -n "Tilt"
skew=`$pamtilt $base.pnm`
skew=`perl -e "if (abs($skew)>0.1 && abs($skew)<3) { print (- $skew) } else { print 0 }"`
echo -n "(${skew})..."
fi
echo -n "Rotate..."
mogrify -quiet \
+dither \
-rotate $skew \
-monochrome \
+matte \
-format tiff \
-compress Group4 \
$base.tiff
\rm -f $base.pnm
echo "done"
}
function print_usage {
cat<<EOF
Usage: $0 [--help] [--nodeskew] [--forcecolor] [--nodeborder] <inputfile> ...
--nodeskew Do not deskew the image (default: deskew)
--forcecolor Do not convert grayscale images to B/W (default: convert to B/W)
--nodeborder Do not attempt to remove the black border (default: remove)
--help Print this message
EOF
exit -1;
}
if [ $# -lt 1 ]; then
print_usage;
fi
while [ $# -gt 0 ]; do
file=$1
shift;
if [ $file == "--help" ]; then
print_usage;
fi
if [ $file == "--nodeskew" ]; then
deskew=no
continue
fi
if [ $file == "--forcecolor" ]; then
forcecolor=yes
continue
fi
if [ $file == "--nodeborder" ]; then
deborder="no";
continue
fi
if [ ! -f $file ]; then
echo "$file does not exist"
continue
fi
if [ $forcecolor == "yes" ]; then
process_color_image $file
else
photoint=`tiffinfo ${file} | grep -i "Photometric Interpretation"`
case $photoint in
*"RGB color") process_color_image $file ;;
*) process_gray_image $file ;;
esac
fi
done
|