forked from ocropus/hocr-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhocr-pdf
executable file
·137 lines (129 loc) · 5.28 KB
/
hocr-pdf
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
#!/usr/bin/python
#
# Copyright 2013 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Create a searchable PDF from a pile of HOCR + JPEG. Tested with
# Tesseract.
import sys
import glob
import os.path
import cStringIO
import base64
import zlib
import re
from PIL import Image
from reportlab.pdfgen.canvas import Canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from xml.etree.ElementTree import ElementTree, ParseError
def export_pdf(playground, default_dpi):
"""Create a searchable PDF from a pile of HOCR + JPEG"""
load_invisible_font()
pdf = Canvas(sys.stdout, pageCompression=1)
pdf.setCreator('hocr-tools')
pdf.setTitle(os.path.basename(playground))
images = sorted(glob.glob(os.path.join(playground, '*.jpg')))
dpi = default_dpi
for image in images:
im = Image.open(image)
w, h = im.size
try:
dpi = im.info['dpi'][0]
except KeyError:
pass
width = w * 72 / dpi
height = h * 72 / dpi
pdf.setPageSize((width, height))
pdf.drawImage(image, 0, 0, width=width, height=height)
add_text_layer(pdf, image, height, dpi)
pdf.showPage()
pdf.save()
def add_text_layer(pdf, image, height, dpi):
"""Draw an invisible text layer for OCR data"""
p1 = re.compile('bbox((\s+\d+){4})')
p2 = re.compile('baseline((\s+[\d\.\-]+){2})')
hocrfile = os.path.splitext(image)[0] + ".hocr"
hocr = ElementTree()
hocr.parse(hocrfile)
for line in hocr.findall(".//{http://www.w3.org/1999/xhtml}span"):
if line.attrib['class'] != 'ocr_line':
continue
linebox = p1.search(line.attrib['title']).group(1).split()
try:
baseline = p2.search(line.attrib['title']).group(1).split()
except AttributeError:
baseline = [ 0, 0 ]
linebox = [float(i) for i in linebox]
baseline = [float(i) for i in baseline]
for word in line:
if word.attrib['class'] != 'ocrx_word':
continue
if word.text is not None:
rawtext = word.text.strip()
else:
try:
innerword = word[0]
if innerword.text is not None:
rawtext = innerword.text.strip()
except:
continue
font_width = pdf.stringWidth(rawtext, 'invisible', 8)
if font_width <= 0:
continue
box = p1.search(word.attrib['title']).group(1).split()
box = [float(i) for i in box]
b = polyval(baseline, (box[0] + box[2]) / 2 - linebox[0]) + linebox[3]
text = pdf.beginText()
text.setTextRenderMode(3) # double invisible
text.setFont('invisible', 8)
text.setTextOrigin(box[0] * 72 / dpi, height - b * 72 / dpi)
box_width = (box[2] - box[0]) * 72 / dpi
text.setHorizScale(100.0 * box_width / font_width)
text.textLine(rawtext)
pdf.drawText(text)
def polyval(poly, x):
return x * poly[0] + poly[1]
# Glyphless variation of vedaal's invisible font retrieved from
# http://www.angelfire.com/pr/pgpf/if.html, which says:
# 'Invisible font' is unrestricted freeware. Enjoy, Improve, Distribute freely
def load_invisible_font():
font = """
eJzdlk1sG0UUx/+zs3btNEmrUKpCPxikSqRS4jpfFURUagmkEQQoiRXgAl07Y3vL2mvt2ml8APXG
hQPiUEGEVDhWVHyIC1REPSAhBOWA+BCgSoULUqsKcWhVBKjhzfPU+VCi3Flrdn7vzZv33ryZ3TUE
gC6chsTx8fHck1ONd98D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KmRTFYVo7CiJug09E+GJb+QocMCPMWBK1wvEOfRFF2U0klK8CppqqvG
pylRc2Zn+XDQWZIL8iO5KC9S+1RekOex1uOyZGR/w/Hf1lhzqVfFsxE39B/ws7Rm3N3nDrhPuMfc
w3R/aE28KsfY2J+RPNp+j+KaOoCey4h+Dd48b9O5G0v2K7j0AM6s+5WQ/E0wVoK+pA6/3bup7bJf
CMGjwvxTsr74/f/F95m3TH9x8o0/TU//N+7/D/ScVcA=
"""
ttf = cStringIO.StringIO(zlib.decompress(base64.decodestring(font)))
pdfmetrics.registerFont(TTFont('invisible', ttf))
if __name__ == "__main__":
if len(sys.argv) == 1:
print("Usage: %s <imgdir>\n" % os.path.basename(sys.argv[0]))
else:
export_pdf(sys.argv[1], 300)