Fixed ocr image cropping

Thanks @mfunkey
This commit is contained in:
Maxim Devaev 2022-01-29 10:22:22 +03:00
parent 8e2bd4265a
commit de842f12e2

View File

@ -139,14 +139,17 @@ class TesseractOcr:
with _tess_api(langs) as api: with _tess_api(langs) as api:
assert _libtess assert _libtess
with io.BytesIO(data) as bio: with io.BytesIO(data) as bio:
with PilImage.open(bio) as image: image = PilImage.open(bio)
try:
if left >= 0 or top >= 0 or right >= 0 or bottom >= 0: if left >= 0 or top >= 0 or right >= 0 or bottom >= 0:
left = (0 if left < 0 else min(image.width, left)) left = (0 if left < 0 else min(image.width, left))
top = (0 if top < 0 else min(image.height, top)) top = (0 if top < 0 else min(image.height, top))
right = (image.width if right < 0 else min(image.width, right)) right = (image.width if right < 0 else min(image.width, right))
bottom = (image.height if bottom < 0 else min(image.height, bottom)) bottom = (image.height if bottom < 0 else min(image.height, bottom))
if left < right and top < bottom: if left < right and top < bottom:
image.crop((left, top, right, bottom)) image_cropped = image.crop((left, top, right, bottom))
image.close()
image = image_cropped
_libtess.TessBaseAPISetImage(api, image.tobytes("raw", "RGB"), image.width, image.height, 3, image.width * 3) _libtess.TessBaseAPISetImage(api, image.tobytes("raw", "RGB"), image.width, image.height, 3, image.width * 3)
text_ptr = None text_ptr = None
@ -159,3 +162,5 @@ class TesseractOcr:
finally: finally:
if text_ptr is not None: if text_ptr is not None:
libc.free(text_ptr) libc.free(text_ptr)
finally:
image.close()