From d3d5879911fb3a8fc3c8e0232ddbccbf7d3fa77a Mon Sep 17 00:00:00 2001 From: Jim Barlow Date: Wed, 8 Apr 2015 21:55:23 -0700 Subject: [PATCH] Replace pdfimages -list call to poppler with PyPDF test for image The immediate reason for doing this is that (newer?) versions of parse() seem to choke on the parse string. It appears to trigger exponential behavior in the underlying regex. In any case, replacing subprocesses with native Python is usually better. --- src/ocrpage.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/ocrpage.py b/src/ocrpage.py index 6dff89d2..59f566e6 100755 --- a/src/ocrpage.py +++ b/src/ocrpage.py @@ -145,22 +145,21 @@ def pdf_get_pageinfo(infile, page, width_pt, height_pt): else: pageinfo['has_text'] = False - # pdfimages: get image dimensions - p_pdfimages = Popen(['pdfimages', '-list', '-f', str(page), '-l', - str(page), str(infile)], close_fds=True, stdout=PIPE, - stderr=PIPE, universal_newlines=True) - pdfimages, _ = p_pdfimages.communicate() - for n, line in enumerate(pdfimages.splitlines()): - if n <= 1: - continue # Skip first two lines + pdf = pypdf.PdfFileReader(infile) + page = pdf.pages[page - 1] - r = parse('{page:1d} {num:1d} {imtype:>} {width:1d} {height:1d} ' + - '{color:>} {comp:1d} {bpc:1d} {enc:>} {interp:>} ' + - '{pdfobject:1d} {pdfid:1d} {bad_dpi_w:1d} {bad_dpi_h:1d} ' + - '{size:>} {ratio:>}', line) - image = r.named - # pdfimages calculates DPI as of 0.26.0, but adds +1 to dpi_h - # apparent bug, so calculate explicitly + if not '/XObject' in page['/Resources']: + # Missing /XObject means no images or possibly corrupt PDF + return pageinfo + + for xobj in page['/Resources']['/XObject']: + # PyPDF2 returns the keys as an iterator + pdfimage = page['/Resources']['/XObject'][xobj] + if pdfimage['/Subtype'] != '/Image': + continue + image = {} + image['width'] = pdfimage['/Width'] + image['height'] = pdfimage['/Height'] image['dpi_w'] = image['width'] / pageinfo['width_inches'] image['dpi_h'] = image['height'] / pageinfo['height_inches'] image['dpi'] = (image['dpi_w'] * image['dpi_h']) ** 0.5