import cv2 import numpy as np from PIL import Image import io def preprocess_image(image_bytes: bytes, max_dim: int = 1500) -> Image.Image: np_img = np.frombuffer(image_bytes, np.uint8) img = cv2.imdecode(np_img, cv2.IMREAD_COLOR) h, w = img.shape[:2] if max(h, w) > max_dim: scale = max_dim / max(h, w) img = cv2.resize(img, (int(w*scale), int(h*scale)), interpolation=cv2.INTER_AREA) img = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21) lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB) l, a, b = cv2.split(lab) clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) l = clahe.apply(l) img = cv2.merge((l, a, b)) img = cv2.cvtColor(img, cv2.COLOR_LAB2BGR) _, encoded = cv2.imencode('.jpg', img) return Image.open(io.BytesIO(encoded.tobytes())).convert("RGB")