Datasets:
Download scripts/generate_image_Task_Geometric.py from Obedience-Violin/Violin: direct link, hf CLI and curl.
- Browser
- Download file 1.51 kB
-
https://huggingface.co/datasets/Obedience-Violin/Violin/resolve/main/scripts/generate_image_Task_Geometric.py
- Command line
-
hf download hf://datasets/Obedience-Violin/Violin/scripts/generate_image_Task_Geometric.py
-
curl -L -o generate_image_Task_Geometric.py https://huggingface.co/datasets/Obedience-Violin/Violin/resolve/main/scripts/generate_image_Task_Geometric.py
1.51 kB
| 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" | |
| ) | |