diff --git a/ocrmypdf/leptonica.py b/ocrmypdf/leptonica.py index 6303a8bf..ff905f57 100644 --- a/ocrmypdf/leptonica.py +++ b/ocrmypdf/leptonica.py @@ -201,6 +201,11 @@ def correlation_binary(pix1, pix2): return correlation[0] +def rotate180(pix): + pix_rotated = lept.pixRotate180(ffi.NULL, pix) + return ffi.gc(pix_rotated, _pix_destroy) + + if __name__ == '__main__': parser = argparse.ArgumentParser( description="Python wrapper to access Leptonica") diff --git a/ocrmypdf/lib/compile_leptonica.py b/ocrmypdf/lib/compile_leptonica.py index c4d10251..27362875 100644 --- a/ocrmypdf/lib/compile_leptonica.py +++ b/ocrmypdf/lib/compile_leptonica.py @@ -53,6 +53,7 @@ void pixDestroy ( PIX **ppix ); PIX * pixDeskew ( PIX *pixs, l_int32 redsearch ); char * getLeptonicaVersion ( ); l_int32 pixCorrelationBinary(PIX *pix1, PIX *pix2, l_float32 *pval); +PIX *pixRotate180(PIX *pixd, PIX *pixs); """) if __name__ == '__main__': diff --git a/tests/test_main.py b/tests/test_main.py index 6f214cc1..c4e900f0 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -261,10 +261,14 @@ def test_autorotate(spoof_tesseract_cache, renderer): xres=100, yres=100, raster_device='pngmono', log=gslog, pageno=1) pix_ref = leptonica.pixRead(_outfile('reference.png')) + pix_ref_180 = leptonica.rotate180(pix_ref) + correlation = leptonica.correlation_binary(pix_ref, pix_ref_180) + assert correlation < 0.10 + for n in range(1, 4+1): pix_other = leptonica.pixRead(_outfile('cardinal-%i.png' % n)) - print(leptonica.correlation_binary(pix_ref, pix_other)) - + correlation = leptonica.correlation_binary(pix_ref, pix_other) + assert correlation > 0.80 @pytest.mark.parametrize('renderer', [ 'hocr',