-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstitchPDF.sh
executable file
·327 lines (226 loc) · 7.75 KB
/
stitchPDF.sh
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/bin/bash
#mount proc /proc -t proc
set -euo pipefail
IFS=$'\n\t'
cpus=$(nproc --all)
echo $1 $2 $3
echo $* > /tmp/stitchPDF
#export PYTHONPATH=/home/mq20151400/exporters/FAIMSScanner/
#echo ${2}ScanRecord/Files/$3
IFS=$'\n'
cd $2
#FIX
rm -rf pdf/$3
mkdir -p pdf/$3
cd pdf/$3
echo -n " Finding Identifier \"$3\". "
if [ -z "$4" ]; then
echo "*no argument for orientation*"
orientation=90
paper="landscape"
scanOrient="left"
else
echo -n "Orientation: $4. "
orientation=$4
if [ "$orientation" -eq "0" -o "$orientation" -eq "180" ]; then
paper="portrait"
else
paper="landscape"
fi
case "$4" in
0)
scanOrient="none"
;;
90)
scanOrient="left"
;;
180)
scanOrient="upsidedown"
;;
270)
scanOrient="right"
;;
esac
fi
numFiles=$(find ../../ -name "$3" -type d | xargs -I{} find {} -name "*.jpg" | wc -l)
echo -n "Working with $numFiles images. "
find ../../ -name "$3" -type d | xargs -I{} find {} -name "*.jpg" ! -name '.*' -print0 | xargs -0 -I{} identify {} | cut -d' ' -f3 | awk -F x -- '/[0-9]/ {print "\\definepapersize[sheet][width=" $1"px,height=" $2 "px]"}' | uniq > geometry
geometry=$(cat geometry)
echo -n "Geometry: $geometry. ScanOrient: ${scanOrient}. "
rm geometry
#find ../../ -name "$3" -type d | xargs -I{} find {} -name "*.jpg" ! -name '.*' | sort -V
#Brian plan. Copy jpgs in and rotate them first.
#find ../../ScanRecord/Files/$3 -name "*.jpg"| sort -V | awk -- 'BEGIN{ FS="[/.]+"} {print "convert " $0 " " ++count ".pnm"}' /dev/stdin | bash
find ../../ -name "$3" -type d | xargs -I{} find {} -name "*.jpg" ! -name '.*' | sort -V | awk -- 'BEGIN{ FS="[/.]+"} {print "convert \"" $0 "\" " ++count ".pnm"}' /dev/stdin | parallel --no-notice
echo -n "Scantailor. "
parallel --no-notice "scantailor-cli --orientation=${scanOrient} --despeckle=normal --normalize-illumination --color-mode=black_and_white --dewarping=auto {} ./ ; rm {}" ::: $(find . -name "*.pnm" | sort -V)
rm -rf cache
echo -n "tiff2pdf. "
parallel --no-notice "tiff2pdf -o '{.}.pdf' -z -u m -p 'A4' -F -c 'scanimage+unpaper+tiff2pdf+pdftk+imagemagick+tesseract+exactimage' {} ; rm {}" ::: $(find . -name "*.tif")
# for file in $(find . -name "*.pnm" | sort -V); do
# #echo $file
#
# scantailor-cli --despeckle=normal --normalize-illumination --color-mode=black_and_white --dewarping=auto $file ./
# rm $file
# rm -rf cache
# done
#echo "tiff2pdf"
# for file in $(find . -name "*.tif" | sort -V); do
# #echo $file
# name=$(echo "$file" | cut -d'.' -f2)
# name=".$name"
#
# tiff2pdf -o "$name.pdf" -z -u m -p "A4" -F $name.tif
# rm $file
#done
echo -n "pdf14. "
echo -n "OCR File processing"
pdf14=$(cat <<-'HereDoc'
mv {} {.}.bak;
pdftk {.}.bak dump_data > {.}.info;
pdftk {.}.bak cat output {.}.bk2 flatten;
convert -normalize -density 300 -depth 8 {.}.bk2 {.}.png;
tesseract -l eng -psm 1 {.}.png {} hocr 2> /dev/null;
TXT=$(echo {/.} | sed -e 's/_/./g;s/L/1/;s/R/2/')
tesseract -l eng -psm 1 {.}.png stdout >> $(printf "%03f_ENG.txt" $TXT) 2>/dev/null;
convert {.}.png {.}.jpg;
hocr2pdf -i {.}.jpg -s -o {.}.bk2 < {}.hocr 2> /dev/null;
pdftk {.}.bk2 update_info {.}.info output {} 2< /dev/null;
rm -f {.}.bak {.}.bk2 {.}.info {.}.png {.}.jpg {}.hocr;
echo -n "."
HereDoc
)
parallel "$pdf14" ::: $(find . -name "*.pdf")
#echo "pdf 1.4"
# for file in $(find . -name "*.pdf" | sort -V); do
# #echo $file
# name=$(basename "$file")
# name="${name%.*}"
#
# #echo $name
# mv "$file" "$name.bak"
# pdftk "$name.bak" dump_data > $file.info
# pdftk "$name.bak" cat output "$name.bk2" flatten
#
# convert -normalize -density 300 -depth 8 "$name.bk2" "$name.png"
# tesseract -l eng -psm 1 "$name.png" "$file" hocr 2> /dev/null
# tesseract -l eng -psm 1 "$name.png" stdout >> "${3}ENG.txt" 2> /dev/null
#
#
# convert "$name.png" "$name.jpg"
# hocr2pdf -i "$name.jpg" -s -o "$name.bk2" < "$file.hocr" 2> /dev/null
#
# pdftk "$name.bk2" update_info $file.info output "$name.OCR.pdf"
# rm -f "$name.bak" "$name.bk2" "$file.info"
# rm -f "$name.png" "$name.jpg" "$file.hocr"
#
# done
mkdir -p stage2
echo -e "\n Writing Plain Text: ${3}_ENG.txt"
for file in $(find . -name "*.txt" | sort -g); do
cat $file >> "stage2/${3}_ENG.txt"
rm $file
done
#echo "listing preocr files"
#find .
echo " Writing OCR: ${3}_OCR.pdf"
pdfunite `find . -name "*.pdf"| sort -V` "stage2/${3}_preOCR.pdf"
#FIX
rm *.pdf
#convert `find ../../ScanRecord/Files/$3 -name "*.jpg"| sort -V` -page a4 "stage2/${3}.pdf"
mkdir jpg2pdf
#parallel --jobs $cpus 'convert {} -compress lzw -auto-orient jpg2pdf/{/.}.tiff' ::: $(find ../../ScanRecord/Files/$3 -name "*.jpg")
#for file in $(find "../../ScanRecord/Files/$3" -name "*.jpg" | sort -V ); do
# outfile="jpg2pdf/$(basename -s ".jpg" $file).tiff"
# convert "$file" -compress lzw -auto-orient "$outfile"
#done
#for file in $(find "jpg2pdf/" -name "*.tiff" | sort -V); do
# tiffcp -a $file jpg2pdf/multi.tiff 2> /dev/null
#done
#tiff2pdf -p A4 -F -j -q 90 -f -o "stage2/${3}_preoriginal.pdf" jpg2pdf/multi.tiff
cd jpg2pdf
echo -n " Making originals into PDF. "
imageDir=$(find ../../../ -name "$3" -type d ! -path "pdf" |tr '\n' ',')
cat <<-HereDoc > "${3}.tex"
\enableregime [utf]
\mainlanguage [en]
${geometry}
\setuppapersize[sheet,${paper}][sheet,${paper}]
\switchtobodyfont[modern,48pt]
\setupexternalfigures[directory={${imageDir}}]
\setuplayout[
backspace=0pt,
topspace=0pt,
header=0pt,
footer=0pt,
% width=\pagewidth,
% height=\pageheight
]
\setuppagenumbering[location={}]
\starttext
HereDoc
cp ../../../$3.md .
sed -i 's# -# -#g' $3.md
pandoc $3.md -t ConTeXt >> "${3}.tex"
find ../../../ -name "$3" -type d | xargs -I{} find {} -name "*.jpg" ! -name '.*' -print0 | sort -V -z | xargs -Ixx -0 echo "\startTEXpage\externalfigure[xx][orientation=${orientation}]{}\stopTEXpage" >> "temp.tex"
for path in $(find ../../../ -name "$3" -type d); do
#echo "s#${path}/##g"
sed -i "s#${path}/##g" temp.tex
done
cat temp.tex >> "${3}.tex"
echo "\stoptext" >> "${3}.tex"
mkdir ../log/
context --purgeall --quiet --batchmode "${3}.tex" &> "../log/${3}tex.log"
#ls
mv "${3}.tex" "../log/${3}_preoriginal.tex"
mv "${3}.pdf" "../stage2/${3}_preoriginal.pdf"
cd ..
#ls stage2/*
#FIX
rm -rf jpg2pdf
#mv stage2/* .
cp ../../$3.md stage2/
cp ../../$3.info stage2/
cd stage2
echo "Finishing. "
echo "" >> $3.info
pandoc $3.md -t ConTeXt -s -o "${3}cover.tex"
sed -i 's/\[letter\]/\[A4\]/g' "${3}cover.tex"
context "${3}cover.tex" --purgeall --quiet --silent --batchmode &> "../log/${3}cover.log"
#pdfunite "${3}cover.pdf" "${3}_preoriginal.pdf" "../${3}_originalfull.pdf"
cp "${3}_preoriginal.pdf" "../${3}_originalfull.pdf"
pdfunite "${3}cover.pdf" "${3}_preOCR.pdf" "../${3}_OCR.pdf"
#pdftk "${3}_originalfull.pdf" dump_data_utf8 output "${3}prefull".info 2>/dev/null
#sed -i '/^Info/d' "${3}prefull.info"
#cat "$3.info" "${3}prefull.info" > "${3}full.info"
#cat "${3}.info"
#echo "***"
#pdftk "${3}_originalfull.pdf" update_info_utf8 "${3}.info" output "../$3_original.pdf"
# pdftk "${3}_OCRfull.pdf" dump_data > "${3}preOCRfull".info 2>/dev/null
# cat "${3}prefull.info"
# sed -i '/^Info/d' "${3}prefull.info"
# sed -i '/^Info/d' "${3}preOCRfull.info"
# cat "$3.info" "${3}prefull.info" > "${3}full.info"
# cat "$3.info" "${3}preOCRfull.info" > "${3}OCRfull.info"
#
# cd ..
#
# echo "eng"
# cat stage2/$3.md "stage2/${3}_ENG.txt" > "${3}_ENG.txt"
# echo $?
#
# echo "orig"
# cat "stage2/${3}full.info"
# pdftk "stage2/${3}_originalfull.pdf" update_info_utf8 "stage2/${3}full.info" output "$3_original.pdf"
# echo $?
#
# echo "ocr"
# pdftk "stage2/${3}_OCRfull.pdf" update_info_utf8 "stage2/${3}OCRfull.info" output "$3_OCR.pdf"
# echo $?
cd ..
cat stage2/$3.md "stage2/${3}_ENG.txt" > "${3}_ENG.txt"
#FIX
rm -rf stage2
echo "* \`${3}\`"
ls | sed -e 's/^/ * `/' | sed -e 's/$/`/'
exit 0