From 20aabb2e838eb52b2e75189ace05908d4cc1d3c7 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Thu, 10 May 2018 01:16:11 -0700 Subject: [PATCH] When deciding if there is a text on a page, ignore the margins Margins may include watermarks or digital stamps on otherwise text-free pages. --- src/ocrmypdf/pdfinfo.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/ocrmypdf/pdfinfo.py b/src/ocrmypdf/pdfinfo.py index bef0f60b..548deb5a 100644 --- a/src/ocrmypdf/pdfinfo.py +++ b/src/ocrmypdf/pdfinfo.py @@ -583,11 +583,31 @@ def _naive_find_text(*, pdf, page): def _page_has_text(infile, pageno): + "Smarter text detection that ignores text in margins" + doc = fitz.Document(infile) - text = doc.getPageText(pageno) - if text.strip() != '': - return True - return False + text = doc[pageno].getText('dict') + if not text: + return + + pw, ph = text['width'], text['height'] + + margin_ratio = 0.125 + interior_bbox = fitz.Rect( + margin_ratio * pw, margin_ratio * ph, + (1 - margin_ratio) * pw, (1 - margin_ratio) * ph + ) + + has_text = False + for block in text['blocks']: + if block['type'] != 0: + continue # Not text + bbox = fitz.Rect(block['bbox']) + + if bbox & interior_bbox: + has_text = True + + return has_text def _pdf_get_pageinfo(pdf, pageno: int, infile):