From ccb1e347be58f03eb73cb02a5140a52945893967 Mon Sep 17 00:00:00 2001 From: Jim Barlow Date: Fri, 20 Feb 2015 17:20:48 -0800 Subject: [PATCH] Call HocrTransform directly instead of through a subprocess --- src/hocrtransform.py | 15 ++++++------ src/ocrpage.py | 57 +++++++++----------------------------------- 2 files changed, 18 insertions(+), 54 deletions(-) diff --git a/src/hocrtransform.py b/src/hocrtransform.py index 7babe89c..2799b821 100755 --- a/src/hocrtransform.py +++ b/src/hocrtransform.py @@ -6,15 +6,17 @@ # Initial version by Jonathan Brinley, jonathanbrinley@gmail.com ############################################################################## from reportlab.pdfgen.canvas import Canvas -from reportlab.pdfgen.pdfimages import PDFImage from reportlab.lib.units import inch from lxml import etree as ElementTree from PIL import Image import re -import sys import argparse +class HocrTransformError(Exception): + pass + + class HocrTransform(): """ @@ -46,12 +48,9 @@ class HocrTransform(): # there shouldn't be more than one, and if there is, we don't want # it break + if self.width is None or self.height is None: + raise HocrTransformError("hocr file is missing page dimensions") - # no width and heigh definition in the ocr_image element of the hocr - # file - if self.width is None: - print("No page dimension found in the hocr file") - sys.exit(1) def __str__(self): """ @@ -109,7 +108,7 @@ class HocrTransform(): s = s.replace(u"fi", "fi") return s - def to_pdf(self, outFileName, imageFileName, showBoundingboxes, fontname="Helvetica"): + def to_pdf(self, outFileName, imageFileName=None, showBoundingboxes=False, fontname="Helvetica"): """ 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 diff --git a/src/ocrpage.py b/src/ocrpage.py index 3579d284..f59ed28b 100755 --- a/src/ocrpage.py +++ b/src/ocrpage.py @@ -1,8 +1,6 @@ #!/usr/bin/env python3 # Reimplement ocrPage.sh as Python -import argparse -import logging import sys import os.path import fileinput @@ -17,11 +15,12 @@ except ImportError: import os DEVNULL = open(os.devnull, 'wb') -from tempfile import NamedTemporaryFile from ruffus import transform, suffix, merge, active_if, regex, jobs_limit, \ mkdir, formatter import ruffus.cmdline as cmdline +from .hocrtransform import HocrTransform + basedir = os.path.dirname(os.path.realpath(__file__)) @@ -572,57 +571,23 @@ def select_image_for_pdf(infiles, output_file): @merge([ocr_tesseract, select_image_for_pdf], os.path.join(options.tmp_fld, '%04i.rendered.pdf' % pageno)) def render_page(infiles, output_file): - # Call python in a subprocess because: - # -That is python2 and this is python3 - # -It is written as a standalone script; not meant for import yet - args_hocrTransform = [ - 'python3', - os.path.join(basedir, 'hocrtransform.py'), - '-r', str(round(max(pageinfo['xres'], pageinfo['yres']))), - '-i', infiles[1], - infiles[0], - output_file - ] - p = Popen(args_hocrTransform, close_fds=True, stdout=PIPE, stderr=PIPE, - universal_newlines=True) - stdout, stderr = p.communicate() + hocr, image = infiles[0], infiles[1] - with logger_mutex: - if stdout: - logger.info(stdout) - if stderr: - logger.error(stderr) + dpi = round(max(pageinfo['xres'], pageinfo['yres'])) - if p.returncode != 0: - raise CalledProcessError(p.returncode, args_hocrTransform) + hocrtransform = HocrTransform(hocr, dpi) + hocrtransform.to_pdf(output_file, imageFileName=image, + showBoundingboxes=False) @active_if(ocr_required and options.pdf_noimg) @transform(ocr_tesseract, suffix(".hocr"), ".ocred.todebug.pdf") def render_text_output_page(input_file, output_file): - # Call python in a subprocess because: - # -That is python2 and this is python3 - # -It is written as a standalone script; not meant for import yet - args_hocrTransform = [ - 'python3', - os.path.join(basedir, 'hocr3ransform.py'), - '-b', - '-r', str(round(max(pageinfo['xres'], pageinfo['yres']))), - input_file, - output_file - ] - p = Popen(args_hocrTransform, close_fds=True, stdout=PIPE, stderr=PIPE, - universal_newlines=True) - stdout, stderr = p.communicate() + dpi = round(max(pageinfo['xres'], pageinfo['yres'])) - with logger_mutex: - if stdout: - logger.info(stdout) - if stderr: - logger.error(stderr) - - if p.returncode != 0: - raise CalledProcessError(p.returncode, args_hocrTransform) + hocrtransform = HocrTransform(input_file, dpi) + hocrtransform.to_pdf(output_file, imageFileName=None, + showBoundingboxes=True) @merge([render_page, skip_ocr],