| import os | |
| from PIL import Image | |
| from tqdm import tqdm | |
| def align_to_16(path): | |
| print(f"🛠️ 正在对 {path} 进行物理对齐(居中裁剪至16倍数)...") | |
| if not os.path.exists(path): | |
| print("❌ 目录不存在!") | |
| return | |
| files = sorted([f for f in os.listdir(path) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]) | |
| for f in tqdm(files): | |
| img_path = os.path.join(path, f) | |
| with Image.open(img_path) as img: | |
| w, h = img.size | |
| # 计算最大的 16 倍数 | |
| new_w = (w // 16) * 16 | |
| new_h = (h // 16) * 16 | |
| if w != new_w or h != new_h: | |
| # 居中裁剪逻辑 | |
| left = (w - new_w) // 2 | |
| top = (h - new_h) // 2 | |
| right = left + new_w | |
| bottom = top + new_h | |
| img.crop((left, top, right, bottom)).save(img_path) | |
| # 执行物理修复 | |
| align_to_16("/root/autodl-tmp/dataset/deepblending_clean/DrJohnson/images") | |