From 2296ac0feedf8af946fb320c5c519ca99b38de7a Mon Sep 17 00:00:00 2001 From: AuroraWright Date: Wed, 24 Jan 2024 17:14:12 +0100 Subject: [PATCH] Hi, I'm stupid --- owocr/ocr.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/owocr/ocr.py b/owocr/ocr.py index 26e885f..b8f6e53 100644 --- a/owocr/ocr.py +++ b/owocr/ocr.py @@ -5,6 +5,7 @@ from pathlib import Path import time import sys import platform +from math import sqrt import jaconv import numpy as np @@ -192,14 +193,11 @@ class GoogleLens: 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) + if w * h > 3000000: + aspect_ratio = w/h + new_w = int(sqrt(3000000 * aspect_ratio)) + new_h = int(new_w / aspect_ratio) + img = img.resize((new_w, new_h), Image.LANCZOS) image_bytes = io.BytesIO() img.save(image_bytes, format='png')