2017-01-26 12:28:51 -08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# © 2017 James R. Barlow: github.com/jbarlow83
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from ocrmypdf.exceptions import ExitCode
|
|
|
|
from ocrmypdf.exec import tesseract
|
2017-05-19 15:48:23 -07:00
|
|
|
from ocrmypdf import pdfinfo
|
2017-03-24 13:23:03 -07:00
|
|
|
import sys
|
2017-03-29 23:31:44 -07:00
|
|
|
import os
|
|
|
|
import PyPDF2 as pypdf
|
2017-06-13 13:09:12 -07:00
|
|
|
from contextlib import contextmanager
|
2017-03-29 23:31:44 -07:00
|
|
|
|
|
|
|
|
|
|
|
spoof = pytest.helpers.spoof
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def ensure_tess4():
|
2017-05-12 00:40:00 -07:00
|
|
|
if tesseract.v4():
|
|
|
|
# Either "tesseract" on $PATH is already v4, or
|
|
|
|
# OCRMYPDF_TESSERACT is tess4 already
|
|
|
|
return os.environ.copy()
|
|
|
|
|
|
|
|
if os.environ.get('OCRMYPDF_TESS4'):
|
|
|
|
# OCRMYPDF_TESS4 is a hint environment variable that tells us to look
|
|
|
|
# somewhere special for tess4 if and only if we need it. This allows
|
|
|
|
# setting OCRMYPDF_TESS4 to test tess4 and PATH to point to tess3
|
|
|
|
# on a system with both installed.
|
|
|
|
env = os.environ.copy()
|
|
|
|
env['OCRMYPDF_TESSERACT'] = env['OCRMYPDF_TESS4']
|
|
|
|
return env
|
|
|
|
|
|
|
|
raise EnvironmentError("Can't find Tesseract 4")
|
2017-03-29 23:31:44 -07:00
|
|
|
|
|
|
|
|
2017-06-13 13:09:12 -07:00
|
|
|
@contextmanager
|
|
|
|
def modified_os_environ(env):
|
|
|
|
old_env = os.environ.copy()
|
|
|
|
os.environ = env
|
|
|
|
yield
|
|
|
|
os.environ = old_env
|
|
|
|
|
|
|
|
|
2017-03-29 23:31:44 -07:00
|
|
|
def tess4_available():
|
|
|
|
"""Check if a tesseract 4 binary is available, even if it's not the
|
|
|
|
official "tesseract" on PATH
|
|
|
|
|
|
|
|
"""
|
|
|
|
try:
|
2017-06-13 13:09:12 -07:00
|
|
|
# ensure_tess4 locates the tess4 binary we are going to check
|
|
|
|
env = ensure_tess4()
|
|
|
|
with modified_os_environ(env):
|
|
|
|
# Now jump into this environment and make sure it really is Tess4
|
|
|
|
return tesseract.v4() and tesseract.has_textonly_pdf()
|
2017-05-12 00:40:00 -07:00
|
|
|
except EnvironmentError:
|
|
|
|
pass
|
2017-01-26 12:28:51 -08:00
|
|
|
|
2017-05-12 00:40:00 -07:00
|
|
|
return False
|
2017-01-26 12:28:51 -08:00
|
|
|
|
|
|
|
# Skip all tests in this file if not tesseract 4
|
2017-03-24 13:23:03 -07:00
|
|
|
pytestmark = pytest.mark.skipif(
|
2017-03-29 23:31:44 -07:00
|
|
|
not tess4_available(),
|
2017-03-24 13:23:03 -07:00
|
|
|
reason="tesseract 4.0 with textonly_pdf feature required")
|
|
|
|
|
|
|
|
check_ocrmypdf = pytest.helpers.check_ocrmypdf
|
|
|
|
run_ocrmypdf = pytest.helpers.run_ocrmypdf
|
|
|
|
spoof = pytest.helpers.spoof
|
2017-01-26 12:28:51 -08:00
|
|
|
|
|
|
|
|
2017-03-29 23:31:44 -07:00
|
|
|
def test_textonly_pdf(ensure_tess4, resources, outdir):
|
2017-03-24 13:23:03 -07:00
|
|
|
check_ocrmypdf(
|
2017-01-26 16:38:59 -08:00
|
|
|
resources / 'linn.pdf',
|
2017-03-29 23:31:44 -07:00
|
|
|
outdir / 'linn_textonly.pdf', '--pdf-renderer', 'tess4',
|
2017-05-10 15:22:44 -07:00
|
|
|
'--sidecar', 'foo',
|
2017-03-29 23:31:44 -07:00
|
|
|
env=ensure_tess4)
|
2017-01-26 12:28:51 -08:00
|
|
|
|
|
|
|
|
2017-03-29 23:31:44 -07:00
|
|
|
def test_pagesize_consistency_tess4(ensure_tess4, resources, outpdf):
|
2017-03-24 13:23:03 -07:00
|
|
|
from math import isclose
|
|
|
|
|
|
|
|
infile = resources / 'linn.pdf'
|
|
|
|
|
|
|
|
before_dims = pytest.helpers.first_page_dimensions(infile)
|
|
|
|
|
|
|
|
check_ocrmypdf(
|
|
|
|
infile,
|
|
|
|
outpdf, '--pdf-renderer', 'tess4',
|
2017-03-29 23:31:44 -07:00
|
|
|
'--clean', '--deskew', '--remove-background', '--clean-final',
|
|
|
|
env=ensure_tess4)
|
2017-03-24 13:23:03 -07:00
|
|
|
|
|
|
|
after_dims = pytest.helpers.first_page_dimensions(outpdf)
|
2017-01-26 12:28:51 -08:00
|
|
|
|
2017-03-24 13:23:03 -07:00
|
|
|
assert isclose(before_dims[0], after_dims[0])
|
|
|
|
assert isclose(before_dims[1], after_dims[1])
|
2017-03-29 23:31:44 -07:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('basename', ['graph_ocred.pdf', 'cardinal.pdf'])
|
|
|
|
def test_skip_pages_does_not_replicate(
|
|
|
|
ensure_tess4, resources, basename, outdir):
|
|
|
|
infile = resources / basename
|
|
|
|
outpdf = outdir / basename
|
|
|
|
|
|
|
|
check_ocrmypdf(
|
|
|
|
infile,
|
|
|
|
outpdf, '--pdf-renderer', 'tess4', '--force-ocr',
|
|
|
|
'--tesseract-timeout', '0',
|
|
|
|
env=ensure_tess4
|
|
|
|
)
|
|
|
|
|
2017-05-19 15:48:23 -07:00
|
|
|
info_in = pdfinfo.PdfInfo(infile)
|
2017-03-29 23:31:44 -07:00
|
|
|
|
2017-05-19 15:48:23 -07:00
|
|
|
info = pdfinfo.PdfInfo(outpdf)
|
2017-03-29 23:31:44 -07:00
|
|
|
for page in info:
|
2017-05-29 13:51:21 -07:00
|
|
|
assert len(page.images) == 1, "skipped page was replicated"
|
2017-03-29 23:31:44 -07:00
|
|
|
|
|
|
|
for n in range(len(info_in)):
|
2017-05-29 13:51:21 -07:00
|
|
|
assert info[n].width_inches == info_in[n].width_inches
|
2017-03-29 23:44:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_content_preservation(ensure_tess4, resources, outpdf):
|
|
|
|
infile = resources / 'masks.pdf'
|
|
|
|
|
|
|
|
check_ocrmypdf(
|
|
|
|
infile,
|
|
|
|
outpdf, '--pdf-renderer', 'tess4', '--tesseract-timeout', '0',
|
|
|
|
env=ensure_tess4
|
|
|
|
)
|
|
|
|
|
2017-05-19 15:48:23 -07:00
|
|
|
info = pdfinfo.PdfInfo(outpdf)
|
2017-03-29 23:44:12 -07:00
|
|
|
page = info[0]
|
2017-05-29 13:51:21 -07:00
|
|
|
assert len(page.images) > 1, "masks were rasterized"
|