import csv import math import os from PIL import Image, ImageDraw IMG_SIZE = 512 def draw_shape(draw, shape, cx, cy, size_ratio): side = size_ratio * IMG_SIZE if shape == "circle": r = side / 2 draw.ellipse([cx - r, cy - r, cx + r, cy + r], fill=255) elif shape == "square": h = side / 2 draw.rectangle([cx - h, cy - h, cx + h, cy + h], fill=255) elif shape == "equilateral triangle": h = side * math.sqrt(3) / 2 pts = [ (cx, cy - h / 2), # apex (cx - side / 2, cy + h / 2), # bottom-left (cx + side / 2, cy + h / 2), # bottom-right ] draw.polygon(pts, fill=255) def generate_images(csv_file, output_dir): 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() shape = row["shape"].strip() cx = float(row["center_x"]) cy = float(row["center_y"]) size_ratio = float(row["size_ratio"]) img = Image.new("L", (IMG_SIZE, IMG_SIZE), 0) draw = ImageDraw.Draw(img) draw_shape(draw, shape, cx, cy, size_ratio) img.save(os.path.join(output_dir, f"id_{id_val}.png")) print(f"Done: {output_dir} ({len(rows)} images)") if __name__ == "__main__": generate_images( "metadata/Task_Geometric_metadata.csv", output_dir="data/Task_Geometric" )