OCRmyPDF/tests/test_preprocessing.py

164 lines
4.9 KiB
Python
Raw Permalink Normal View History

2022-07-28 01:06:46 -07:00
# SPDX-FileCopyrightText: 2022 James R. Barlow
# SPDX-License-Identifier: MPL-2.0
2019-12-09 15:52:38 -08:00
2022-07-23 00:39:24 -07:00
from __future__ import annotations
from math import isclose
2019-12-09 15:52:38 -08:00
import pytest
from PIL import Image
from ocrmypdf._exec import ghostscript, tesseract
from ocrmypdf.exceptions import ExitCode
2020-04-24 04:12:05 -07:00
from ocrmypdf.helpers import Resolution
2019-12-09 15:52:38 -08:00
from ocrmypdf.pdfinfo import PdfInfo
from .conftest import check_ocrmypdf, have_unpaper, run_ocrmypdf
2019-12-09 15:52:38 -08:00
RENDERERS = ['hocr', 'sandwich']
2020-06-01 03:06:40 -07:00
def test_deskew(resources, outdir):
2019-12-09 15:52:38 -08:00
# Run with deskew
deskewed_pdf = check_ocrmypdf(resources / 'skew.pdf', outdir / 'skew.pdf', '-d')
2019-12-09 15:52:38 -08:00
2021-11-13 00:06:35 -08:00
# Now render as an image again...
2019-12-09 15:52:38 -08:00
deskewed_png = outdir / 'deskewed.png'
ghostscript.rasterize_pdf(
2020-04-24 04:12:05 -07:00
deskewed_pdf,
deskewed_png,
raster_device='pngmono',
raster_dpi=Resolution(150, 150),
pageno=1,
2019-12-09 15:52:38 -08:00
)
2021-11-13 00:06:35 -08:00
# ...and use Tessera to find the skew angle to confirm that it was deskewed
skew_angle = tesseract.get_deskew(deskewed_png, [], None, 5.0)
2019-12-09 15:52:38 -08:00
print(skew_angle)
assert -0.5 < skew_angle < 0.5, "Deskewing failed"
def test_deskew_blank_page(resources, outpdf):
# Tesseract doesn't like blank pages - make sure we can get through
check_ocrmypdf(resources / 'blank.pdf', outpdf, '--deskew')
@pytest.mark.xfail(reason="remove background disabled")
2020-06-01 03:06:40 -07:00
def test_remove_background(resources, outdir):
2019-12-09 15:52:38 -08:00
# Ensure the input image does not contain pure white/black
with Image.open(resources / 'baiona_color.jpg') as im:
2019-12-09 15:52:38 -08:00
assert im.getextrema() != ((0, 255), (0, 255), (0, 255))
output_pdf = check_ocrmypdf(
resources / 'baiona_color.jpg',
2019-12-09 15:52:38 -08:00
outdir / 'test_remove_bg.pdf',
'--remove-background',
'--image-dpi',
'150',
2020-06-01 03:06:40 -07:00
'--plugin',
'tests/plugins/tesseract_noop.py',
2019-12-09 15:52:38 -08:00
)
output_png = outdir / 'remove_bg.png'
ghostscript.rasterize_pdf(
2020-04-24 04:12:05 -07:00
output_pdf,
output_png,
raster_device='png16m',
raster_dpi=Resolution(100, 100),
pageno=1,
2019-12-09 15:52:38 -08:00
)
# The output image should contain pure white and black
with Image.open(output_png) as im:
assert im.getextrema() == ((0, 255), (0, 255), (0, 255))
# This will run 5 * 2 * 2 = 20 test cases
@pytest.mark.parametrize(
"pdf", ['palette.pdf', 'cmyk.pdf', 'ccitt.pdf', 'jbig2.pdf', 'lichtenstein.pdf']
)
@pytest.mark.parametrize("renderer", ['sandwich', 'hocr'])
@pytest.mark.parametrize("output_type", ['pdf', 'pdfa'])
def test_exotic_image(pdf, renderer, output_type, resources, outdir):
2019-12-09 15:52:38 -08:00
outfile = outdir / f'test_{pdf}_{renderer}.pdf'
check_ocrmypdf(
resources / pdf,
outfile,
2021-04-07 01:56:51 -07:00
'-dc' if have_unpaper() else '-d',
2019-12-09 15:52:38 -08:00
'-v',
'1',
'--output-type',
output_type,
'--sidecar',
'--skip-text',
'--pdf-renderer',
renderer,
'--plugin',
'tests/plugins/tesseract_cache.py',
2019-12-09 15:52:38 -08:00
)
assert outfile.with_suffix('.pdf.txt').exists()
@pytest.mark.parametrize('renderer', RENDERERS)
def test_non_square_resolution(renderer, resources, outpdf):
2019-12-09 15:52:38 -08:00
# Confirm input image is non-square resolution
in_pageinfo = PdfInfo(resources / 'aspect.pdf')
2020-04-24 04:12:05 -07:00
assert in_pageinfo[0].dpi.x != in_pageinfo[0].dpi.y
2019-12-09 15:52:38 -08:00
proc = run_ocrmypdf(
2019-12-09 15:52:38 -08:00
resources / 'aspect.pdf',
outpdf,
'--pdf-renderer',
renderer,
'--plugin',
'tests/plugins/tesseract_cache.py',
2019-12-09 15:52:38 -08:00
)
# PDF/A conversion can fail for this file if Ghostscript >= 10.3, so don't test
# exit code in that case
if proc.returncode != ExitCode.pdfa_conversion_failed:
proc.check_returncode()
2019-12-09 15:52:38 -08:00
out_pageinfo = PdfInfo(outpdf)
# Confirm resolution was kept the same
2020-04-24 04:12:05 -07:00
assert in_pageinfo[0].dpi == out_pageinfo[0].dpi
2019-12-09 15:52:38 -08:00
@pytest.mark.parametrize('renderer', RENDERERS)
def test_convert_to_square_resolution(renderer, resources, outpdf):
2019-12-09 15:52:38 -08:00
# Confirm input image is non-square resolution
in_pageinfo = PdfInfo(resources / 'aspect.pdf')
2020-04-24 04:12:05 -07:00
assert in_pageinfo[0].dpi.x != in_pageinfo[0].dpi.y
2019-12-09 15:52:38 -08:00
# --force-ocr requires means forced conversion to square resolution
check_ocrmypdf(
resources / 'aspect.pdf',
outpdf,
'--force-ocr',
'--pdf-renderer',
renderer,
'--plugin',
'tests/plugins/tesseract_cache.py',
2019-12-09 15:52:38 -08:00
)
out_pageinfo = PdfInfo(outpdf)
in_p0, out_p0 = in_pageinfo[0], out_pageinfo[0]
# Resolution show now be equal
2020-04-24 04:12:05 -07:00
assert out_p0.dpi.x == out_p0.dpi.y
2019-12-09 15:52:38 -08:00
# Page size should match input page size
assert isclose(in_p0.width_inches, out_p0.width_inches)
assert isclose(in_p0.height_inches, out_p0.height_inches)
# Because we rasterized the page to produce a new image, it should occupy
# the entire page
2020-04-24 04:12:05 -07:00
out_im_w = out_p0.images[0].width / out_p0.images[0].dpi.x
out_im_h = out_p0.images[0].height / out_p0.images[0].dpi.y
2019-12-09 15:52:38 -08:00
assert isclose(out_p0.width_inches, out_im_w)
assert isclose(out_p0.height_inches, out_im_h)