From 9229f7c6cc2306fe16d628bd984f2e9afb2843c0 Mon Sep 17 00:00:00 2001 From: Jim Barlow Date: Sat, 21 Feb 2015 14:19:27 -0800 Subject: [PATCH] Add option to render text as invisible OCR text Prior to this change, hocrtransform would render printable text (black on white) and then a fully opaque image on top of the text. According to the PDF spec, text that is the output of OCR should be marked invisible, so that PDF viewers /know/ it's OCR output in a document that might mix OCR and text overlays. Another benefit is that PDF viewers would know to skip rendering text if they are not smart enough to figure out the image will completely overwrite it. However, for debug, visible text is nice, so retain it as an option. --- src/hocrtransform.py | 9 ++++++--- src/ocrpage.py | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/hocrtransform.py b/src/hocrtransform.py index 2446de49..dea4306c 100755 --- a/src/hocrtransform.py +++ b/src/hocrtransform.py @@ -50,8 +50,9 @@ class HocrTransform(): for div in self.hocr.findall( ".//%sdiv[@class='ocr_page']" % (self.xmlns)): coords = self.element_coordinates(div) - self.width = self.px2pt(coords[2] - coords[0]) - self.height = self.px2pt(coords[3] - coords[1]) + pt_coords = self.pt_from_pixel(coords) + self.width = pt_coords.x2 - pt_coords.x1 + self.height = pt_coords.y2 - pt_coords.y1 # there shouldn't be more than one, and if there is, we don't want # it break @@ -116,7 +117,7 @@ class HocrTransform(): return s def to_pdf(self, outFileName, imageFileName=None, showBoundingboxes=False, - fontname="Helvetica"): + fontname="Helvetica", invisibleText=False): """ Creates a PDF file with an image superimposed on top of the text. Text is positioned according to the bounding box of the lines in @@ -187,6 +188,8 @@ class HocrTransform(): text = pdf.beginText() fontsize = pt.y2 - pt.y1 text.setFont(fontname, fontsize) + if invisibleText: + text.setTextRenderMode(3) # Invisible (indicates OCR text) # set cursor to bottom left corner of bbox (adjust for dpi) text.setTextOrigin(pt.x1, self.height - pt.y2) diff --git a/src/ocrpage.py b/src/ocrpage.py index f59ed28b..99f6b050 100755 --- a/src/ocrpage.py +++ b/src/ocrpage.py @@ -577,7 +577,7 @@ def render_page(infiles, output_file): hocrtransform = HocrTransform(hocr, dpi) hocrtransform.to_pdf(output_file, imageFileName=image, - showBoundingboxes=False) + showBoundingboxes=False, invisibleText=True) @active_if(ocr_required and options.pdf_noimg) @@ -587,7 +587,7 @@ def render_text_output_page(input_file, output_file): hocrtransform = HocrTransform(input_file, dpi) hocrtransform.to_pdf(output_file, imageFileName=None, - showBoundingboxes=True) + showBoundingboxes=True, invisibleText=False) @merge([render_page, skip_ocr],