import os
import pandas as pd
from PIL import Image, ImageDraw, ImageFont
import random

# Define directories for images
base_dir = "dataset"
cats_dir = os.path.join(base_dir, "cats")
dogs_dir = os.path.join(base_dir, "dogs")

# Create directories if they don't exist
os.makedirs(cats_dir, exist_ok=True)
os.makedirs(dogs_dir, exist_ok=True)

# Function to generate a synthetic image with a label
def generate_image(file_path, label, img_size=(64, 64)):
    # Create a blank image with a random background color
    bg_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
    image = Image.new("RGB", img_size, bg_color)
    
    # Add text to indicate cat or dog
    draw = ImageDraw.Draw(image)
    try:
        # Use a default font (adjust path if needed for your system)
        font = ImageFont.truetype("arial.ttf", 20)
    except:
        # Fallback to default PIL font if truetype font is unavailable
        font = ImageFont.load_default()
    
    # Add "Cat" or "Dog" text
    text = label
    text_bbox = draw.textbbox((0, 0), text, font=font)
    text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]
    text_position = ((img_size[0] - text_width) // 2, (img_size[1] - text_height) // 2)
    draw.text(text_position, text, fill="white", font=font)
    
    # Save image
    image.save(file_path)

# Generate sample images and collect data
num_images_per_class = 100  # Adjust as needed for demo
image_data = []

# Generate cat images
for i in range(num_images_per_class):
    file_name = f"cat_{i}.jpg"
    file_path = os.path.join(cats_dir, file_name)
    generate_image(file_path, "Cat")
    image_data.append({"image_path": file_path, "label": 0})

# Generate dog images
for i in range(num_images_per_class):
    file_name = f"dog_{i}.jpg"
    file_path = os.path.join(dogs_dir, file_name)
    generate_image(file_path, "Dog")
    image_data.append({"image_path": file_path, "label": 1})

# Create DataFrame and save to CSV
df = pd.DataFrame(image_data)
csv_path = "cats_dogs.csv"
df.to_csv(csv_path, index=False)
print(f"Created {csv_path} with {len(df)} entries.")

# Optional: Print sample of the DataFrame
print("\nSample of cats_dogs.csv:")
print(df.head())

