Automate the generation of 30 prompts

user1272047user1272047
3 min read

To automate the generation of 30 prompts and their corresponding images, you can use a Python script that interacts with the OpenAI API (for DALL·E) and generates structured prompts based on your dialogue sequences.


Solution: Automating Prompt & Image Generation

  1. Create a structured format for the 60 dialogues (paired in twos).

  2. Define a prompt template that adapts dynamically to each dialogue.

  3. Use a Python script to:

    • Read dialogue pairs.

    • Generate a corresponding image prompt.

    • Call the DALL·E API to generate the image.

    • Save the image output for reference.


Python Script for Automation

This script will:

  • Take 60 dialogues (30 pairs) from a text file.

  • Auto-generate prompts based on your preferred style.

  • Call OpenAI's DALL·E API to generate and save images.

1️⃣ Prepare the dialogues

Create a text file (dialogues.txt) where each line contains a dialogue pair:

Bob: "I found a lottery ticket!"
Mona: "Are you sure it’s still valid?"
Max: "I’m getting a skateboard with rockets!"
Lisa: "Check the expiration date first!"
...

2️⃣ Run the script

This Python script will automatically create prompts and generate images:

import openai
import time

# OpenAI API key (replace with your actual API key)
openai.api_key = "YOUR_OPENAI_API_KEY"

# Read dialogues from file
with open("dialogues.txt", "r", encoding="utf-8") as f:
    lines = f.readlines()

# Group dialogues in pairs
dialogue_pairs = [lines[i:i+2] for i in range(0, len(lines), 2)]

# Function to generate a prompt for an image
def generate_prompt(dialogue_pair, index):
    characters = [
        "A middle-aged man with short brown hair, round belly, and a wide grin holding a lottery ticket.",
        "A worried woman with tall blue hair, a green dress, and pearl necklace, looking skeptical.",
        "A mischievous young boy with short, spiky hair, large round eyes, and an excited grin.",
        "A smart-looking girl with shoulder-length hair, wearing a red dress and pearl necklace, crossing her arms skeptically."
    ]

    background = "A cozy living room with a couch and family pictures on the wall."

    prompt = f"Digital painting of a cartoon-style scene with yellow-skinned characters. {characters[index % len(characters)]} \
    {dialogue_pair[0].strip()} {dialogue_pair[1].strip()} The background is {background}. With gradient shading, clean linework, \
    vibrant palette, and stylized proportions. Low resolution."

    return prompt

# Function to generate an image using OpenAI API
def generate_image(prompt, index):
    response = openai.Image.create(
        prompt=prompt,
        n=1,
        size="1024x1024"
    )

    image_url = response["data"][0]["url"]

    # Save prompt for reference
    with open(f"prompt_{index}.txt", "w", encoding="utf-8") as f:
        f.write(prompt)

    print(f"Generated Image {index + 1}: {image_url}")

# Loop through dialogues and generate images
for i, dialogue_pair in enumerate(dialogue_pairs):
    prompt = generate_prompt(dialogue_pair, i)
    generate_image(prompt, i)
    time.sleep(5)  # Prevent API rate limits

3️⃣ How it Works

  • Reads dialogues from dialogues.txt (pairs them).

  • Generates a customized prompt for each scene (adjusts characters).

  • Sends requests to OpenAI’s DALL·E API for image generation.

  • Saves both the prompt and image link for future reference.

  • Uses time.sleep(5) to avoid hitting API rate limits.


4️⃣ Running the Script

  1. Install dependencies (if needed):

     pip install openai
    
  2. Run the script:

     python generate_images.py
    
  3. Check the output:

    • Generated image URLs will be printed.

    • Corresponding prompt files (prompt_X.txt) will be saved.


Final Output

  • 30 automatically generated images

  • 30 prompts saved for reuse

  • Dialogue-integrated descriptions


Download the images automatically.

to do

0
Subscribe to my newsletter

Read articles from user1272047 directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

user1272047
user1272047