From 6189910c74b70e4c2c141117fe4b57ace08a5b32 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Thu, 11 Jul 2019 02:20:04 -0700 Subject: [PATCH] Fix text-image registration when mediabox contains an offset Cropbox, trimbox not addressed... should look at those. Also rotation. --- src/ocrmypdf/_graft.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/ocrmypdf/_graft.py b/src/ocrmypdf/_graft.py index 3bdb79ae..6a7cf23a 100644 --- a/src/ocrmypdf/_graft.py +++ b/src/ocrmypdf/_graft.py @@ -21,8 +21,6 @@ from pathlib import Path import pikepdf -from .exec import tesseract - MAX_REPLACE_PAGES = int(os.environ.get('_OCRMYPDF_MAX_REPLACE_PAGES', 100)) @@ -117,6 +115,7 @@ def _graft_text_layer( translate = pikepdf.PdfMatrix().translated(-wt / 2, -ht / 2) untranslate = pikepdf.PdfMatrix().translated(wp / 2, hp / 2) + corner = pikepdf.PdfMatrix().translated(mediabox[0], mediabox[1]) # -rotation because the input is a clockwise angle and this formula # uses CCW rotation = -rotation % 360 @@ -134,8 +133,9 @@ def _graft_text_layer( scale = pikepdf.PdfMatrix().scaled(scale_x, scale_y) # Translate the text so it is centered at (0, 0), rotate it there, adjust - # for a size different between initial and text PDF, then untranslate - ctm = translate @ rotate @ scale @ untranslate + # for a size different between initial and text PDF, then untranslate, and + # finally move the lower left corner to match the mediabox + ctm = translate @ rotate @ scale @ untranslate @ corner pdf_text_contents = b'q %s cm\n' % ctm.encode() + pdf_text_contents + b'\nQ\n' @@ -221,8 +221,9 @@ class OcrGrafter: text_rotation = autorotate_correction text_misaligned = (text_rotation - content_rotation) % 360 self.log.debug( - '%r', - [text_rotation, autorotate_correction, text_misaligned, content_rotation], + f"Rotations for page {pageno}: [text, auto, misalign, content] = " + f"{text_rotation}, {autorotate_correction}, " + f"{text_misaligned}, {content_rotation}" ) if text and self.font: