Repository navigation
Expand file tree
/
Copy pathconvert
More file actions
105 lines (87 loc) · 2.45 KB
/
Copy pathconvert
File metadata and controls
105 lines (87 loc) · 2.45 KB
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
#!/usr/bin/env zsh
#
runname=`basename $0`
case $runname in
ebook)
# docx to md
d2m () {
pandoc -f docx -t markdown --extract-media=$1 -o $1.md $1.docx
}
# md to docx
m2d () {
pandoc $1.md -o $1.docx
}
# md to pdf
m2p () {
pandoc --pdf-engine=xelatex \
--toc -N \
-V colorlinks -V urlcolor=NavyBlue \
--highlight-style zenburn \
$1.md -o $1.pdf
}
# md to epub
m2e () {
pandoc -s --toc --toc-depth=2 $1.md -o $1.epub
}
# png to pdf
p2p () {
sips -s format pdf $1.png --out $1.pdf
}
;;
w2m)
eval "$(rbenv init -)"
export PATH="/opt/homebrew/lib/ruby/gems/3.0.0/bin:/opt/homebrew/opt/ruby/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/ruby/lib"
export CPPFLAGS="-I/opt/homebrew/opt/ruby/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/ruby/lib/pkgconfig"
;;
ocr)
source ~/Init/abrew
;;
*)
;;
esac
pdfocr () {
name=$1
# Split and convert the PDF with ImageMagick convert
convert -density 300 $name.pdf -type Grayscale -compress lzw -background white +matte -depth 32 page_%05d.tif
# OCR the pages with Tesseract
for i in page_*.tif; do echo $i; tesseract $i $(basename $i .tif) pdf; done
# Join your individual PDF files into a single, searchable PDF with pdftk
pdftk page_*.pdf cat output ${name}_merged.pdf
}
ocrpdf () {
source ~/Init/abrew
while test "x$1" != "x"; do
case $1 in
-l) shift; lang=$1 ;;
-i) shift; inpu=$1 ;;
-o) shift; oupt=$1 ;;
-f) shift; ofmt=$1 ;;
*) ;;
esac
shift
done
if [[ $lang == "" ]]; then lang=eng+chi_sim; fi
if [[ $inpu == "" ]]; then inpu=input; fi
if [[ $oupt == "" ]]; then oupt=${inpu}_o; fi
if [[ $ofmt == "" ]]; then ofmt=pdf; fi
ocrmypdf -l $lang --output-type $ofmt $inpu.pdf $oupt.pdf
}
ocrpng () {
sips -s format pdf $1.png --out $1.pdf
ocrpdf -i $1
}
ocr () {
name=(`get_suffix $1`)
prefix=${name[1]}
suffix=${name[2]}
case $suffix in
pdf)
ocrpdf $prefix
;;
png)
ocrpng $prefix
;;
esac
}