OCRmyPDF/tests/test_image_input.py

90 lines
2.4 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-10 01:06:27 -08:00
2022-07-23 00:39:24 -07:00
from __future__ import annotations
2019-12-10 01:06:27 -08:00
from unittest.mock import patch
import img2pdf
import pikepdf
2019-12-19 15:29:56 -08:00
import pytest
from PIL import Image
2019-12-10 01:06:27 -08:00
import ocrmypdf
2021-04-07 01:56:51 -07:00
from .conftest import check_ocrmypdf, run_ocrmypdf_api
2019-12-10 01:06:27 -08:00
2021-04-07 02:09:45 -07:00
# pylint: disable=redefined-outer-name
2019-12-10 01:06:27 -08:00
@pytest.fixture
def baiona(resources):
return Image.open(resources / 'baiona_gray.png')
2020-06-01 03:06:40 -07:00
def test_image_to_pdf(resources, outpdf):
2019-12-10 01:06:27 -08:00
check_ocrmypdf(
2020-06-01 03:06:40 -07:00
resources / 'crom.png',
outpdf,
'--image-dpi',
'200',
'--plugin',
'tests/plugins/tesseract_noop.py',
2019-12-10 01:06:27 -08:00
)
def test_no_dpi_info(caplog, baiona, outdir, no_outpdf):
im = baiona
assert 'dpi' not in im.info
input_image = outdir / 'baiona_no_dpi.png'
im.save(input_image)
rc = run_ocrmypdf_api(input_image, no_outpdf)
assert rc == ocrmypdf.ExitCode.input_file
assert "--image-dpi" in caplog.text
def test_dpi_not_credible(caplog, baiona, outdir, no_outpdf):
im = baiona
assert 'dpi' not in im.info
input_image = outdir / 'baiona_no_dpi.png'
im.save(input_image, dpi=(30, 30))
rc = run_ocrmypdf_api(input_image, no_outpdf)
assert rc == ocrmypdf.ExitCode.input_file
assert "not credible" in caplog.text
def test_cmyk_no_icc(caplog, resources, no_outpdf):
rc = run_ocrmypdf_api(resources / 'baiona_cmyk.jpg', no_outpdf)
assert rc == ocrmypdf.ExitCode.input_file
assert "no ICC profile" in caplog.text
def test_img2pdf_fails(resources, no_outpdf):
with patch(
'ocrmypdf._pipeline.img2pdf.convert', side_effect=img2pdf.ImageOpenError()
) as mock:
2019-12-10 01:06:27 -08:00
rc = run_ocrmypdf_api(
resources / 'baiona_gray.png', no_outpdf, '--image-dpi', '200'
)
assert rc == ocrmypdf.ExitCode.input_file
mock.assert_called()
2019-12-10 01:06:27 -08:00
@pytest.mark.xfail(reason="remove background disabled")
2020-06-01 03:06:40 -07:00
def test_jpeg_in_jpeg_out(resources, outpdf):
2019-12-10 01:06:27 -08:00
check_ocrmypdf(
resources / 'baiona_color.jpg',
2019-12-10 01:06:27 -08:00
outpdf,
'--image-dpi',
'100',
'--output-type',
'pdf', # specifically check pdf because Ghostscript may convert to JPEG
'--remove-background',
2020-06-01 03:06:40 -07:00
'--plugin',
'tests/plugins/tesseract_noop.py',
2019-12-10 01:06:27 -08:00
)
with pikepdf.open(outpdf) as pdf:
assert next(iter(pdf.pages[0].images.values())).Filter == pikepdf.Name.DCTDecode