2017-01-26 12:28:51 -08:00
|
|
|
# © 2017 James R. Barlow: github.com/jbarlow83
|
2018-03-14 14:40:48 -07:00
|
|
|
#
|
|
|
|
# This file is part of OCRmyPDF.
|
|
|
|
#
|
|
|
|
# OCRmyPDF is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# OCRmyPDF is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with OCRmyPDF. If not, see <http://www.gnu.org/licenses/>.
|
2017-01-26 12:28:51 -08:00
|
|
|
|
|
|
|
import pytest
|
2018-11-09 01:40:01 -08:00
|
|
|
from ocrmypdf.exceptions import ExitCode, MissingDependencyError
|
2017-01-26 12:28:51 -08:00
|
|
|
from ocrmypdf.exec import tesseract
|
2018-11-09 01:40:01 -08:00
|
|
|
from ocrmypdf.helpers import fspath
|
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
|
2018-03-24 15:07:02 -07:00
|
|
|
from pathlib import Path
|
2017-03-29 23:31:44 -07:00
|
|
|
|
2018-02-24 12:38:58 -08:00
|
|
|
# pylint: disable=no-member
|
2017-03-29 23:31:44 -07:00
|
|
|
spoof = pytest.helpers.spoof
|
|
|
|
|
|
|
|
|
2018-10-03 15:04:46 -07:00
|
|
|
def _ensure_tess4():
|
2017-05-12 00:40:00 -07:00
|
|
|
if tesseract.v4():
|
2018-03-24 15:07:02 -07:00
|
|
|
# "tesseract" on $PATH is already v4
|
2017-05-12 00:40:00 -07:00
|
|
|
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()
|
2018-03-24 15:07:02 -07:00
|
|
|
tess4 = Path(os.environ['OCRMYPDF_TESS4'])
|
|
|
|
assert tess4.is_file()
|
|
|
|
env['PATH'] = tess4.parent + ':' + env['PATH']
|
2018-11-09 01:40:01 -08:00
|
|
|
env['OCRMYPDF_TESS4'] = os.environ['OCRMYPDF_TESS4']
|
2017-05-12 00:40:00 -07:00
|
|
|
return env
|
|
|
|
|
|
|
|
raise EnvironmentError("Can't find Tesseract 4")
|
2017-03-29 23:31:44 -07:00
|
|
|
|
|
|
|
|
2018-10-03 15:04:46 -07:00
|
|
|
@pytest.fixture
|
|
|
|
def ensure_tess4():
|
|
|
|
return _ensure_tess4()
|
|
|
|
|
|
|
|
|
2017-06-13 13:09:12 -07:00
|
|
|
@contextmanager
|
|
|
|
def modified_os_environ(env):
|
|
|
|
old_env = os.environ.copy()
|
2018-11-09 01:40:01 -08:00
|
|
|
os.environ.update(env)
|
2017-06-13 13:09:12 -07:00
|
|
|
yield
|
2018-11-09 01:40:01 -08:00
|
|
|
for key in env:
|
|
|
|
del os.environ[key]
|
|
|
|
if key in old_env:
|
|
|
|
os.environ[key] = old_env[key]
|
2017-06-13 13:09:12 -07:00
|
|
|
|
|
|
|
|
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:
|
2018-10-03 15:04:46 -07:00
|
|
|
# _ensure_tess4 locates the tess4 binary we are going to check
|
|
|
|
env = _ensure_tess4()
|
2017-06-13 13:09:12 -07:00
|
|
|
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',
|
2018-03-26 01:17:22 -07:00
|
|
|
outdir / 'linn_textonly.pdf', '--pdf-renderer', 'sandwich',
|
2018-03-24 22:34:35 -07:00
|
|
|
'--sidecar', outdir / 'foo.txt',
|
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,
|
2018-03-26 01:17:22 -07:00
|
|
|
outpdf, '--pdf-renderer', 'sandwich',
|
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,
|
2018-03-26 01:17:22 -07:00
|
|
|
outpdf, '--pdf-renderer', 'sandwich', '--force-ocr',
|
2017-03-29 23:31:44 -07:00
|
|
|
'--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,
|
2018-03-26 01:17:22 -07:00
|
|
|
outpdf, '--pdf-renderer', 'sandwich', '--tesseract-timeout', '0',
|
2017-03-29 23:44:12 -07:00
|
|
|
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]
|
2018-02-24 12:38:58 -08:00
|
|
|
assert len(page.images) > 1, "masks were rasterized"
|
2018-11-09 01:40:01 -08:00
|
|
|
|
|
|
|
|
|
|
|
def test_no_languages(ensure_tess4, tmpdir):
|
|
|
|
env = ensure_tess4
|
|
|
|
(tmpdir / 'tessdata').mkdir()
|
|
|
|
env['TESSDATA_PREFIX'] = fspath(tmpdir)
|
|
|
|
|
|
|
|
with modified_os_environ(env):
|
|
|
|
with pytest.raises(MissingDependencyError):
|
|
|
|
tesseract.languages.cache_clear()
|
|
|
|
tesseract.languages()
|