diff --git a/ocrmypdf/leptonica.py b/ocrmypdf/leptonica.py index f1a1d632..5d86fadd 100644 --- a/ocrmypdf/leptonica.py +++ b/ocrmypdf/leptonica.py @@ -213,6 +213,11 @@ class Pix: with LeptonicaErrorTrap(): return Pix(lept.pixRotate180(ffi.NULL, self._pix)) + def rotate_orth(self, quads): + "Orthographic rotation, quads: 0-3, number of clockwise rotations" + with LeptonicaErrorTrap(): + return Pix(lept.pixRotateOrth(self._pix, quads)) + def find_skew(self): """Returns a tuple (deskew angle in degrees, confidence value). @@ -286,6 +291,40 @@ class Pix: return None return Pix(thresh_pix) + def crop_to_foreground( + self, threshold=128, mindist=70, erasedist=30, pagenum=0, + showmorph=0, display=0, pdfdir=ffi.NULL): + with LeptonicaErrorTrap(): + cropbox = Box(lept.pixFindPageForeground( + self._pix, + threshold, + mindist, + erasedist, + pagenum, + showmorph, + display, + pdfdir)) + + print(repr(cropbox)) + + cropped_pix = lept.pixClipRectangle( + self._pix, + cropbox._box, + ffi.NULL) + + return Pix(cropped_pix) + + def clean_background_to_white( + self, mask=None, grayscale=None, gamma=1.0, black=0, white=255): + with LeptonicaErrorTrap(): + return Pix(lept.pixCleanBackgroundToWhite( + self._pix, + mask or ffi.NULL, + grayscale or ffi.NULL, + gamma, + black, + white)) + @staticmethod @lru_cache(maxsize=1) def make_pixel_sum_tab8(): @@ -329,6 +368,43 @@ class Pix: # print('pix destroy ' + repr(pix)) +class Box: + """Wrapper around Leptonica's BOX objects. + + See class Pix for notes about reference counting. + """ + + def __init__(self, box): + self._box = ffi.gc(box, Box._box_destroy) + + def __repr__(self): + if self._box: + return ''.format( + self.x, self.y, self.w, self.h) + return '' + + @property + def x(self): + return self._box.x + + @property + def y(self): + return self._box.y + + @property + def w(self): + return self._box.w + + @property + def h(self): + return self._box.h + + @staticmethod + def _box_destroy(box): + p_box = ffi.new('BOX **', box) + lept.boxDestroy(p_box) + + @lru_cache(maxsize=1) def get_leptonica_version(): """Get Leptonica version string. diff --git a/ocrmypdf/lib/compile_leptonica.py b/ocrmypdf/lib/compile_leptonica.py index 3a540f2d..6f9fde5e 100644 --- a/ocrmypdf/lib/compile_leptonica.py +++ b/ocrmypdf/lib/compile_leptonica.py @@ -66,6 +66,9 @@ 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); +PIX * +pixRotateOrth(PIX *pixs, + l_int32 quads); l_int32 pixCountPixels ( PIX *pix, l_int32 *pcount, l_int32 *tab8 ); PIX * pixAnd ( PIX *pixd, PIX *pixs1, PIX *pixs2 ); @@ -101,6 +104,14 @@ pixOtsuThreshOnBackgroundNorm(PIX *pixs, l_float32 scorefract, l_int32 *pthresh); +PIX * +pixCleanBackgroundToWhite(PIX *pixs, + PIX *pixim, + PIX *pixg, + l_float32 gamma, + l_int32 blackval, + l_int32 whiteval); + BOX * pixFindPageForeground(PIX *pixs, l_int32 threshold, @@ -111,6 +122,14 @@ pixFindPageForeground(PIX *pixs, l_int32 display, const char *pdfdir); +PIX * +pixClipRectangle(PIX *pixs, + BOX *box, + BOX **pboxc); + +void +boxDestroy(BOX **pbox); + void lept_free(void *ptr); """)