from PIL import Image, ImageOps, ImageFilter, ImageEnhance import numpy as np def preprocess_floorplan(image, max_side=1400): img=image.convert("RGB") w,h=img.size scale=min(1.0,max_side/max(w,h)) if scale<1.0: img=img.resize((int(w*scale),int(h*scale)),Image.LANCZOS) gray=ImageOps.grayscale(img) gray=ImageEnhance.Contrast(gray).enhance(1.8) gray=gray.filter(ImageFilter.MedianFilter(size=3)) arr=np.array(gray) low=np.percentile(arr,18); high=np.percentile(arr,92) if high<=low: high=low+1 norm=np.clip((arr-low)/(high-low)*255,0,255).astype(np.uint8) clean=ImageOps.autocontrast(Image.fromarray(norm,mode="L")) return Image.merge("RGB",(clean,clean,clean))