From 9041867f865f8527c63ac8637d6d8bef66cc81e1 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Thu, 1 Sep 2016 14:23:31 -0700 Subject: [PATCH] pageinfo: exclude images from DPI calculation if drawn at stack depth 0 More thorough testing showed that Acrobat do not presume that images fill the page if the CTM is unspecified, as tests/resources/masks.pdf seems to want. Instead they treat it literally and draw the image as 1x1 PDF units or 1/72" square in the bottom left corner of the page. Seems like the best thing to do is ignore any such images for the purpose of DPI calculation. masks.pdf still works out okay because it has other images. For more robustness we could consider invalidating any DPI above some limit, or warning the user about these microdot thumbnails. --- ocrmypdf/pageinfo.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/ocrmypdf/pageinfo.py b/ocrmypdf/pageinfo.py index 68afdf60..900e595f 100644 --- a/ocrmypdf/pageinfo.py +++ b/ocrmypdf/pageinfo.py @@ -286,22 +286,16 @@ def _find_page_regular_images(page, pageinfo, contentsinfo): if raster.name != image['name']: continue - shorthand = raster.shorthand - if image['type'] == 'stencil' and raster.stack_depth == 0: - # By observation, it seems that stencil masks are implicitly - # scaled over the whole page if they are ever drawn when the - # graphics stack depth is 0. I can't find this documented - # in the reference manual but it is consistent with how - # Acrobat and OS X Preview behave. I also can't find a - # description, generally, of what should happen when an image - # is drawn with the default CTM. - page_w = float(pageinfo['width_inches']) * 72.0 - page_h = float(pageinfo['height_inches']) * 72.0 - shorthand = (page_w, 0.0, 0.0, - page_h, 0.0, 0.0) + if raster.stack_depth == 0: + # At least one PDF in the wild (and test suite) draws an image + # when the graphics stack depth is 0, meaning that the image + # gets drawn into a square of 1x1 PDF units (or 1/72", + # or 0.35 mm). The equivalent DPI will be >100,000. Exclude + # these from our DPI calculation for the page. + continue dpi_w, dpi_h = _get_dpi( - shorthand, (image['width'], image['height'])) + raster.shorthand, (image['width'], image['height'])) # When image is used multiple times take the highest DPI it is # rendered at