import numpy as np import cv2 from PIL import Image IMAGE_SIZE = 128 def preprocess_for_cnn(image: Image.Image): img = np.array(image.convert("L")) img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE)) img = img.astype(np.float32) / 255.0 img = np.expand_dims(img, axis=(0, -1)) return img