from PIL import Image import os, numpy as np def reset_image(src): img = Image.open(src).convert('RGBA') w, h = img.size arr = np.array(img) non_white = np.where(np.any(arr[:,:,:3] < 230, axis=(1,2)))[0] if len(non_white) == 0: return top = non_white[0] bottom = non_white[-1] if top / h < 0.05: print(f"Skip: {os.path.basename(src)}"); return margin = int(h * 0.03) new_top = max(0, top - margin) new_bot = min(h, bottom + margin) cropped = img.crop((0, new_top, w, new_bot)) cropped.save(src, 'PNG') print(f"Reset: {os.path.basename(src)}") for dirpath, dirs, files in os.walk('/var/www/3dcaketopper.nl/verjaardag/images'): for f in files: if f.lower().endswith('.png'): reset_image(os.path.join(dirpath, f))