2018-04-27 17:21:01 -07:00
|
|
|
# © 2018 James R. Barlow: github.com/jbarlow83
|
|
|
|
#
|
2020-08-05 00:44:42 -07:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-04-27 17:21:01 -07:00
|
|
|
|
|
|
|
|
2019-12-19 15:29:56 -08:00
|
|
|
from os import fspath
|
2018-11-02 01:10:10 -07:00
|
|
|
from pickle import dumps, loads
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from PIL import Image, ImageChops
|
|
|
|
|
2020-07-19 01:50:09 -07:00
|
|
|
from ocrmypdf import leptonica as lp
|
2018-04-27 17:21:01 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_colormap_backgroundnorm(resources):
|
|
|
|
# Issue #262 - unclear how to reproduce exactly, so just ensure leptonica
|
|
|
|
# can handle that case
|
2020-07-19 01:50:09 -07:00
|
|
|
pix = lp.Pix.open(resources / 'baiona_colormapped.png')
|
2018-04-27 17:21:01 -07:00
|
|
|
pix.background_norm()
|
2018-11-02 01:10:10 -07:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def crom_pix(resources):
|
2020-07-19 01:50:09 -07:00
|
|
|
pix = lp.Pix.open(resources / 'crom.png')
|
2018-11-02 01:10:10 -07:00
|
|
|
im = Image.open(resources / 'crom.png')
|
2019-09-03 17:19:12 -07:00
|
|
|
yield pix, im
|
|
|
|
im.close()
|
2018-11-02 01:10:10 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_pix_basic(crom_pix):
|
|
|
|
pix, im = crom_pix
|
|
|
|
|
|
|
|
assert pix.width == im.width
|
|
|
|
assert pix.height == im.height
|
|
|
|
assert pix.mode == im.mode
|
|
|
|
|
|
|
|
|
|
|
|
def test_pil_conversion(crom_pix):
|
|
|
|
pix, im = crom_pix
|
|
|
|
|
|
|
|
# Check for pixel perfect
|
|
|
|
assert ImageChops.difference(pix.topil(), im).getbbox() is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_pix_otsu(crom_pix):
|
|
|
|
pix, _ = crom_pix
|
|
|
|
im1bpp = pix.otsu_adaptive_threshold()
|
|
|
|
assert im1bpp.mode == '1'
|
|
|
|
|
|
|
|
|
2019-11-20 00:29:48 -08:00
|
|
|
@pytest.mark.skipif(
|
2020-07-19 01:50:09 -07:00
|
|
|
lp.get_leptonica_version() < 'leptonica-1.76',
|
2019-11-20 00:29:48 -08:00
|
|
|
reason="needs new leptonica for API change",
|
|
|
|
)
|
2018-11-02 01:10:10 -07:00
|
|
|
def test_crop(resources):
|
2020-07-19 01:50:09 -07:00
|
|
|
pix = lp.Pix.open(resources / 'linn.png')
|
2018-11-02 01:10:10 -07:00
|
|
|
foreground = pix.crop_to_foreground()
|
|
|
|
assert foreground.width < pix.width
|
|
|
|
|
|
|
|
|
2018-11-06 11:15:02 -08:00
|
|
|
def test_clean_bg(resources):
|
2020-07-19 01:50:09 -07:00
|
|
|
pix = lp.Pix.open(resources / 'congress.jpg')
|
2018-11-02 01:10:10 -07:00
|
|
|
imbg = pix.clean_background_to_white()
|
|
|
|
|
|
|
|
|
|
|
|
def test_pickle(crom_pix):
|
|
|
|
pix, _ = crom_pix
|
|
|
|
pickled = dumps(pix)
|
|
|
|
pix2 = loads(pickled)
|
|
|
|
assert pix.mode == pix2.mode
|
2018-11-02 01:55:25 -07:00
|
|
|
|
|
|
|
|
2019-06-01 01:55:51 -07:00
|
|
|
def test_leptonica_compile(tmp_path):
|
2018-11-02 01:55:25 -07:00
|
|
|
from ocrmypdf.lib.compile_leptonica import ffibuilder
|
|
|
|
|
|
|
|
# Compile the library but build it somewhere that won't interfere with
|
|
|
|
# existing compiled library. Also compile in API mode so that we test
|
|
|
|
# the interfaces, even though we use it ABI mode.
|
2019-06-01 01:55:51 -07:00
|
|
|
ffibuilder.compile(tmpdir=fspath(tmp_path), target=fspath(tmp_path / 'lepttest.*'))
|
2019-03-17 14:22:36 -07:00
|
|
|
|
|
|
|
|
2019-11-28 14:41:37 -08:00
|
|
|
def test_file_not_found():
|
|
|
|
with pytest.raises(FileNotFoundError):
|
2020-07-19 01:50:09 -07:00
|
|
|
lp.Pix.open("does_not_exist1")
|
|
|
|
|
|
|
|
|
2020-07-19 21:54:51 -07:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
lp.get_leptonica_version() < 'leptonica-1.79.0',
|
|
|
|
reason="test not reliable on all platforms for old leptonica",
|
|
|
|
)
|
2020-07-19 01:50:09 -07:00
|
|
|
def test_error_trap():
|
|
|
|
with pytest.raises(lp.LeptonicaError, match=r"Error in pixReadMem"):
|
|
|
|
with lp._LeptonicaErrorTrap():
|
|
|
|
lp.Pix(lp.lept.pixReadMem(lp.ffi.NULL, 0))
|