File size: 1,511 Bytes
2535e42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89c0dfe
 
2535e42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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"
    )