diff --git a/ocrmypdf/tesseract.py b/ocrmypdf/tesseract.py index abc62a89..ec315b69 100644 --- a/ocrmypdf/tesseract.py +++ b/ocrmypdf/tesseract.py @@ -147,11 +147,16 @@ def page_timedout(log, input_file): log.warning(prefix + " took too long to OCR - skipping") -def _generate_null_hocr(output_hocr, pageinfo): +def _generate_null_hocr(output_hocr, image): + """Produce a .hocr file that reports no text detected on a page that is + the same size as the input image.""" + from PIL import Image + + im = Image.open(image) + w, h = im.size + with open(output_hocr, 'w', encoding="utf-8") as f: - f.write(HOCR_TEMPLATE.format( - pageinfo['width_pixels'], - pageinfo['height_pixels'])) + f.write(HOCR_TEMPLATE.format(w, h)) def generate_hocr(input_file, output_hocr, language: list, tessconfig: list, @@ -181,11 +186,11 @@ def generate_hocr(input_file, output_hocr, language: list, tessconfig: list, # Temporary workaround to hocrTransform not being able to function if # it does not have a valid hOCR file. page_timedout(log, input_file) - _generate_null_hocr(output_hocr, pageinfo_getter()) + _generate_null_hocr(output_hocr, input_file) except CalledProcessError as e: tesseract_log_output(log, e.output, input_file) if 'Image too large' in e.output: - _generate_null_hocr(output_hocr, pageinfo_getter()) + _generate_null_hocr(output_hocr, input_file) return raise e from e diff --git a/tests/test_main.py b/tests/test_main.py index e528a74a..cdd67638 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -707,6 +707,7 @@ def test_rotated_skew_timeout(): out = check_ocrmypdf( 'rotated_skew.pdf', 'test_rotated_skew.pdf', + '--pdf-renderer', 'hocr', '--deskew', '--tesseract-timeout', '0') out_pageinfo = pdf_get_all_pageinfo(out)[0]