"Fossies" - the Fresh Open Source Software Archive

Member "OCRmyPDF-8.3.0/tests/test_lept.py" (13 May 2019, 3048 Bytes) of package /linux/privat/OCRmyPDF-8.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "test_lept.py": 8.2.2_vs_8.2.3.

    1 # © 2018 James R. Barlow: github.com/jbarlow83
    2 #
    3 # This file is part of OCRmyPDF.
    4 #
    5 # OCRmyPDF is free software: you can redistribute it and/or modify
    6 # it under the terms of the GNU General Public License as published by
    7 # the Free Software Foundation, either version 3 of the License, or
    8 # (at your option) any later version.
    9 #
   10 # OCRmyPDF is distributed in the hope that it will be useful,
   11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 # GNU General Public License for more details.
   14 #
   15 # You should have received a copy of the GNU General Public License
   16 # along with OCRmyPDF.  If not, see <http://www.gnu.org/licenses/>.
   17 
   18 
   19 from os import fspath
   20 import os
   21 from pickle import dumps, loads
   22 from unittest.mock import patch
   23 
   24 import pytest
   25 from PIL import Image, ImageChops
   26 
   27 import ocrmypdf.leptonica as lept
   28 
   29 
   30 def test_colormap_backgroundnorm(resources):
   31     # Issue #262 - unclear how to reproduce exactly, so just ensure leptonica
   32     # can handle that case
   33     pix = lept.Pix.open(resources / 'baiona_colormapped.png')
   34     pix.background_norm()
   35 
   36 
   37 @pytest.fixture
   38 def crom_pix(resources):
   39     pix = lept.Pix.open(resources / 'crom.png')
   40     im = Image.open(resources / 'crom.png')
   41     return pix, im
   42 
   43 
   44 def test_pix_basic(crom_pix):
   45     pix, im = crom_pix
   46 
   47     assert pix.width == im.width
   48     assert pix.height == im.height
   49     assert pix.mode == im.mode
   50 
   51 
   52 def test_pil_conversion(crom_pix):
   53     pix, im = crom_pix
   54 
   55     # Check for pixel perfect
   56     assert ImageChops.difference(pix.topil(), im).getbbox() is None
   57 
   58 
   59 def test_pix_otsu(crom_pix):
   60     pix, _ = crom_pix
   61     im1bpp = pix.otsu_adaptive_threshold()
   62     assert im1bpp.mode == '1'
   63 
   64 
   65 def test_crop(resources):
   66     pix = lept.Pix.open(resources / 'linn.png')
   67     foreground = pix.crop_to_foreground()
   68     assert foreground.width < pix.width
   69 
   70 
   71 def test_clean_bg(resources):
   72     pix = lept.Pix.open(resources / 'congress.jpg')
   73     imbg = pix.clean_background_to_white()
   74 
   75 
   76 def test_pickle(crom_pix):
   77     pix, _ = crom_pix
   78     pickled = dumps(pix)
   79     pix2 = loads(pickled)
   80     assert pix.mode == pix2.mode
   81 
   82 
   83 def test_leptonica_compile(tmpdir):
   84     from ocrmypdf.lib.compile_leptonica import ffibuilder
   85 
   86     # Compile the library but build it somewhere that won't interfere with
   87     # existing compiled library. Also compile in API mode so that we test
   88     # the interfaces, even though we use it ABI mode.
   89     ffibuilder.compile(tmpdir=fspath(tmpdir), target=fspath(tmpdir / 'lepttest.*'))
   90 
   91 
   92 def test_with_stderr(capsys):
   93     # pytest redirects stderr too; we must disable this for the test to be valid
   94     with capsys.disabled():
   95         with pytest.raises(FileNotFoundError):
   96             lept.Pix.open("does_not_exist1")
   97 
   98 
   99 def test_without_stderr(capsys):
  100     # pytest redirects stderr too; we must disable this for the test to be valid
  101     with capsys.disabled():
  102         with patch('sys.stderr', new=None):
  103             with pytest.raises(FileNotFoundError):
  104                 lept.Pix.open("does_not_exist2")