import csv import os from PIL import Image SIZE = (512, 512) MASK_TYPES = ["inpainting", "outpainting", "random"] def apply_mask(image1_path, mask_path, size=SIZE): """Return GT image: keep image1 pixels where mask=1 (white), black where mask=0.""" img = Image.open(image1_path).convert("RGB").resize(size) mask = Image.open(mask_path).convert("L").resize(size) black = Image.new("RGB", size, (0, 0, 0)) return Image.composite(img, black, mask) def generate_gt_images(csv_file, output_dir): if not os.path.exists(csv_file): print(f"Warning: {csv_file} not found, skipping.") return with open(csv_file, "r", encoding="utf-8") as f: rows = list(csv.DictReader(f)) os.makedirs(output_dir, exist_ok=True) for row in rows: id_val = row["id"].strip() image1_path = row.get("image1_path", "").strip() image2_path = row.get("image2_path", "").strip() DATA_ROOT = "data" image1_path = os.path.join(DATA_ROOT, image1_path) image2_path = os.path.join(DATA_ROOT, image2_path) img_path = os.path.join(output_dir, f"id_{id_val}.png") if image1_path and os.path.exists(image1_path): gt = apply_mask(image1_path, image2_path) gt.save(img_path) else: print(f"Skip id={id_val}: image1_path missing or not found ({image1_path!r})") print(f"Done: {output_dir}") if __name__ == "__main__": for mask_type in MASK_TYPES: generate_gt_images( f"metadata/Task_Image_Mask_{mask_type}_metadata.csv", output_dir=f"data/Task_Image_Mask/{mask_type}" )