2015-07-22 02:59:25 -07:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2015-07-25 00:57:07 -07:00
|
|
|
from __future__ import print_function
|
2015-07-27 16:11:51 -07:00
|
|
|
from subprocess import Popen, PIPE, check_output
|
2015-07-22 02:59:25 -07:00
|
|
|
import os
|
2015-07-22 04:00:59 -07:00
|
|
|
import shutil
|
|
|
|
from contextlib import suppress
|
2015-07-22 11:21:33 -07:00
|
|
|
import sys
|
2015-07-27 22:07:04 -07:00
|
|
|
from unittest.mock import patch, create_autospec
|
2015-07-28 00:43:22 -07:00
|
|
|
import pytest
|
2015-07-28 02:31:18 -07:00
|
|
|
from ocrmypdf.pageinfo import pdf_get_all_pageinfo
|
2015-07-27 22:07:04 -07:00
|
|
|
|
2015-07-22 11:21:33 -07:00
|
|
|
|
|
|
|
if sys.version_info.major < 3:
|
|
|
|
print("Requires Python 3.4+")
|
|
|
|
sys.exit(1)
|
2015-07-22 02:59:25 -07:00
|
|
|
|
|
|
|
TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
PROJECT_ROOT = os.path.dirname(TESTS_ROOT)
|
|
|
|
OCRMYPDF = os.path.join(PROJECT_ROOT, 'OCRmyPDF.sh')
|
|
|
|
TEST_RESOURCES = os.path.join(PROJECT_ROOT, 'tests', 'resources')
|
2015-07-22 03:16:19 -07:00
|
|
|
TEST_OUTPUT = os.path.join(PROJECT_ROOT, 'tests', 'output')
|
2015-07-22 02:59:25 -07:00
|
|
|
|
|
|
|
|
2015-07-22 03:16:19 -07:00
|
|
|
def setup_module():
|
2015-07-22 04:00:59 -07:00
|
|
|
with suppress(FileNotFoundError):
|
|
|
|
shutil.rmtree(TEST_OUTPUT)
|
|
|
|
with suppress(FileExistsError):
|
2015-07-22 03:16:19 -07:00
|
|
|
os.mkdir(TEST_OUTPUT)
|
|
|
|
|
|
|
|
|
2015-07-27 15:39:54 -07:00
|
|
|
def run_ocrmypdf_sh(input_file, output_file, *args):
|
2015-07-22 04:00:59 -07:00
|
|
|
sh_args = ['sh', OCRMYPDF] + list(args) + [input_file, output_file]
|
2015-07-22 02:59:25 -07:00
|
|
|
sh = Popen(
|
|
|
|
sh_args, close_fds=True, stdout=PIPE, stderr=PIPE,
|
|
|
|
universal_newlines=True)
|
|
|
|
out, err = sh.communicate()
|
|
|
|
return sh, out, err
|
|
|
|
|
|
|
|
|
2015-07-28 00:43:22 -07:00
|
|
|
def check_ocrmypdf(input_basename, output_basename, *args):
|
2015-07-22 04:00:59 -07:00
|
|
|
input_file = os.path.join(TEST_RESOURCES, input_basename)
|
2015-07-27 22:07:04 -07:00
|
|
|
output_file = os.path.join(TEST_OUTPUT, output_basename)
|
2015-07-22 04:00:59 -07:00
|
|
|
|
2015-07-27 15:39:54 -07:00
|
|
|
sh, _, err = run_ocrmypdf_sh(input_file, output_file, *args)
|
2015-07-22 02:59:25 -07:00
|
|
|
assert sh.returncode == 0, err
|
2015-07-22 04:00:59 -07:00
|
|
|
assert os.path.exists(output_file), "Output file not created"
|
2015-07-27 15:39:54 -07:00
|
|
|
assert os.stat(output_file).st_size > 100, "PDF too small or empty"
|
|
|
|
return output_file
|
2015-07-22 02:59:25 -07:00
|
|
|
|
|
|
|
|
2015-07-28 00:43:22 -07:00
|
|
|
def test_quick():
|
|
|
|
check_ocrmypdf('c02-22.pdf', 'test_quick.pdf')
|
2015-07-22 04:00:59 -07:00
|
|
|
|
|
|
|
|
2015-07-28 00:43:22 -07:00
|
|
|
def test_deskew():
|
2015-07-27 16:11:51 -07:00
|
|
|
# Run with deskew
|
2015-07-28 00:43:22 -07:00
|
|
|
deskewed_pdf = check_ocrmypdf('skew.pdf', 'test_deskew.pdf', '-d')
|
2015-07-27 16:11:51 -07:00
|
|
|
|
|
|
|
# Now render as an image again and use Leptonica to find the skew angle
|
|
|
|
# to confirm that it was deskewed
|
2015-07-27 15:39:54 -07:00
|
|
|
from ocrmypdf.ghostscript import rasterize_pdf
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger()
|
|
|
|
|
|
|
|
deskewed_png = os.path.join(TEST_OUTPUT, 'deskewed.png')
|
|
|
|
|
|
|
|
rasterize_pdf(
|
|
|
|
deskewed_pdf,
|
|
|
|
deskewed_png,
|
|
|
|
xres=150,
|
|
|
|
yres=150,
|
|
|
|
raster_device='pngmono',
|
|
|
|
log=log)
|
|
|
|
|
|
|
|
from ocrmypdf.leptonica import pixRead, pixDestroy, pixFindSkew
|
|
|
|
pix = pixRead(deskewed_png)
|
|
|
|
skew_angle, skew_confidence = pixFindSkew(pix)
|
|
|
|
pix = pixDestroy(pix)
|
|
|
|
|
|
|
|
print(skew_angle)
|
|
|
|
assert -0.5 < skew_angle < 0.5, "Deskewing failed"
|
2015-07-22 04:00:59 -07:00
|
|
|
|
|
|
|
|
2015-07-28 00:43:22 -07:00
|
|
|
def test_clean():
|
|
|
|
check_ocrmypdf('skew.pdf', 'test_clean.pdf', '-c')
|
2015-07-27 15:39:54 -07:00
|
|
|
|
|
|
|
|
2015-07-27 16:11:51 -07:00
|
|
|
def test_metadata():
|
2015-07-28 00:43:22 -07:00
|
|
|
pdf = check_ocrmypdf(
|
2015-07-27 16:11:51 -07:00
|
|
|
'c02-22.pdf', 'test_metadata.pdf',
|
|
|
|
'--title', 'Du siehst den Wald vor lauter Bäumen nicht.',
|
|
|
|
'--author', '孔子',
|
|
|
|
'--subject', 'U+1030C is: 𐌌')
|
|
|
|
|
|
|
|
out_pdfinfo = check_output(['pdfinfo', pdf], universal_newlines=True)
|
|
|
|
lines_pdfinfo = out_pdfinfo.splitlines()
|
|
|
|
pdfinfo = {}
|
|
|
|
for line in lines_pdfinfo:
|
|
|
|
k, v = line.strip().split(':', maxsplit=1)
|
|
|
|
pdfinfo[k.strip()] = v.strip()
|
|
|
|
|
|
|
|
assert pdfinfo['Title'] == 'Du siehst den Wald vor lauter Bäumen nicht.'
|
|
|
|
assert pdfinfo['Author'] == '孔子'
|
|
|
|
assert pdfinfo['Subject'] == 'U+1030C is: 𐌌'
|
|
|
|
assert pdfinfo.get('Keywords', '') == ''
|
2015-07-27 17:18:02 -07:00
|
|
|
|
|
|
|
|
2015-07-27 22:07:04 -07:00
|
|
|
def check_oversample(renderer):
|
2015-07-28 00:43:22 -07:00
|
|
|
oversampled_pdf = check_ocrmypdf(
|
2015-07-27 22:07:04 -07:00
|
|
|
'skew.pdf', 'test_oversample_%s.pdf' % renderer, '--oversample', '300',
|
|
|
|
'--pdf-renderer', renderer)
|
2015-07-27 17:18:02 -07:00
|
|
|
|
|
|
|
pdfinfo = pdf_get_all_pageinfo(oversampled_pdf)
|
|
|
|
|
|
|
|
print(pdfinfo[0]['xres'])
|
|
|
|
assert abs(pdfinfo[0]['xres'] - 300) < 1
|
2015-07-27 22:07:04 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_oversample():
|
|
|
|
yield check_oversample, 'hocr'
|
|
|
|
yield check_oversample, 'tesseract'
|
|
|
|
|
|
|
|
|
|
|
|
def test_repeat_ocr():
|
2015-07-28 00:43:22 -07:00
|
|
|
sh, _, _ = run_ocrmypdf_sh('graph_ocred.pdf', 'wontwork.pdf')
|
|
|
|
assert sh.returncode != 0
|
2015-07-27 22:07:04 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_force_ocr():
|
2015-07-28 02:31:18 -07:00
|
|
|
out = check_ocrmypdf('graph_ocred.pdf', 'test_force.pdf', '-f')
|
|
|
|
pdfinfo = pdf_get_all_pageinfo(out)
|
|
|
|
assert pdfinfo[0]['has_text']
|
2015-07-27 22:07:04 -07:00
|
|
|
|
|
|
|
|
2015-07-28 00:43:22 -07:00
|
|
|
def test_skip_ocr():
|
2015-07-28 03:02:35 -07:00
|
|
|
check_ocrmypdf('graph_ocred.pdf', 'test_skip.pdf', '-s')
|
2015-07-28 01:47:30 -07:00
|
|
|
|
|
|
|
|
|
|
|
def check_ocr_timeout(renderer):
|
2015-07-28 02:31:18 -07:00
|
|
|
out = check_ocrmypdf('skew.pdf', 'test_timeout_%s.pdf' % renderer,
|
|
|
|
'--tesseract-timeout', '1.0')
|
|
|
|
pdfinfo = pdf_get_all_pageinfo(out)
|
|
|
|
assert pdfinfo[0]['has_text'] == False
|
2015-07-28 01:47:30 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_ocr_timeout():
|
|
|
|
yield check_ocr_timeout, 'hocr'
|
|
|
|
yield check_ocr_timeout, 'tesseract'
|
2015-07-28 02:31:18 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_skip_big():
|
|
|
|
out = check_ocrmypdf('enormous.pdf', 'test_enormous.pdf',
|
|
|
|
'--skip-big', '10')
|
|
|
|
pdfinfo = pdf_get_all_pageinfo(out)
|
|
|
|
assert pdfinfo[0]['has_text'] == False
|
|
|
|
|
2015-07-28 03:02:35 -07:00
|
|
|
|
|
|
|
def check_maximum_options(renderer):
|
|
|
|
check_ocrmypdf(
|
|
|
|
'multipage.pdf', 'test_multipage%s.pdf' % renderer,
|
|
|
|
'-d', '-c', '-i', '-g', '-f', '-k', '--oversample', '300',
|
|
|
|
'--skip-big', '10', '--title', 'Too Many Weird Files',
|
|
|
|
'--author', 'py.test', '--pdf-renderer', renderer)
|
|
|
|
|
|
|
|
|
|
|
|
def test_maximum_options():
|
|
|
|
yield check_maximum_options, 'hocr'
|
|
|
|
yield check_maximum_options, 'tesseract'
|