From 4e7d7142973bc64a38ba7ac15af39f2cc42b6694 Mon Sep 17 00:00:00 2001 From: AuroraWright Date: Wed, 24 Jan 2024 14:26:59 +0100 Subject: [PATCH] Resize image to fit within Lens API limits --- owocr/ocr.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/owocr/ocr.py b/owocr/ocr.py index 7583e98..26e885f 100644 --- a/owocr/ocr.py +++ b/owocr/ocr.py @@ -191,6 +191,16 @@ class GoogleLens: return x def _preprocess(self, img): + w,h = img.size + ratio = w/h + while True: + if h * w < 3000000: + if (w,h) != img.size: + img = img.resize((w, h), Image.LANCZOS) + break + h -= 1 + w = int(h * ratio) + image_bytes = io.BytesIO() img.save(image_bytes, format='png') return image_bytes.getvalue()