Fix “deskew-rotate” bug.
Turns out this occurred in any case where pdf-renderer hocr was used and a tesseract timeout or error occurred. We created a replacement page based on the unrotated page dimensions instead of the input image’s dimensions.
This commit is contained in:
+11
-6
@@ -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
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user