Module 4: Image Generation

1. Introduction: Image Generation

Description

This is the first capsule of Module 4 of the Multimodal AI Guide. Here you'll understand the current state of AI image generation, what models exist, how they work at a high level, and what you can build with them. If the earlier modules (vision, documents) focused on understanding visual content, this module focuses on creating visual content from scratch.

Why it matters: Image generation is the most visible "output" modality of multimodal AI. In 2023-2024, DALL-E 3, Midjourney and Stable Diffusion transformed entire industries: marketing, e-commerce, publishing, gaming, and prototyping. However, most developers have only tried these tools casually. Mastering the generation APIs — understanding parameters, costs, limitations and how to combine generation with analysis — sets you apart from the rest.

This module is not a "how to generate pretty images" tutorial. It's a technical guide for integrating image generation into software systems: APIs, parameters, fallbacks between providers, pipelines that combine generation with vision, and architecture decisions (DALL-E or Stable Diffusion? When to use each?).

The module's final project — an Image Generator with fallback — demonstrates resilience against provider failures: if DALL-E fails, the system automatically generates with Stable Diffusion, without the user noticing.


Where Are We in the Guide?

Context

This guide has 8 modules organized into 3 phases:

Phase 1: Multimodal Foundations (Modules 1-3)
├── Module 1: Introduction to Multimodal AI          ✓ completed
├── Module 2: Vision + LLMs                          ✓ completed
└── Module 3: Document Understanding                  ✓ completed

Phase 2: Generation and Audio (Modules 4-5)
├── Module 4: Image Generation                       ← YOU ARE HERE
└── Module 5: Audio Processing

Phase 3: RAG, Use Cases and Project (Modules 6-8)
├── Module 6: Multimodal RAG
├── Module 7: Use Cases
└── Module 8: Final Project — Multimodal Document Analyzer

Total estimated duration: 6-7 hours (self-paced).

Where are we headed?

In modules 1-3 you learned to analyze content: send images to GPT-4 Vision, extract data from PDFs, classify documents. Now we reverse the direction: instead of image → text, we do text → image.

The progression within this module is deliberate:

  1. First you understand the landscape (this capsule) — models, providers, use cases
  2. Then you master DALL-E 3 (capsule 2) — the most accessible and highest-quality option
  3. Then Stable Diffusion (capsule 3) — more control, cheaper, open source
  4. You compare both (capsule 4) — data-driven architecture decisions
  5. You design prompts for images (capsule 5) — visual prompt engineering
  6. You edit and create variations (capsule 6) — inpainting, outpainting, variations
  7. You integrate into pipelines (capsule 7) — generation + vision in one flow
  8. You build the project (capsule 8) — a complete Image Generator with fallback

The Era of Image Generation

A brief history

AI image generation has a short but intense history:

2021 — DALL-E (original)
  OpenAI presents a model that generates images from text.
  Impressive but limited. Access for researchers only.

2022 — Stable Diffusion + Midjourney + DALL-E 2
  Stable Diffusion is released as open source → community explosion.
  Midjourney gains popularity for artistic quality.
  DALL-E 2 launches a public API.

2023 — DALL-E 3 + SDXL
  DALL-E 3 integrated into ChatGPT. Dramatically improved prompt understanding.
  SDXL (Stable Diffusion XL) raises the quality of the open source ecosystem.

2024 — SD3 + Flux + massive competition
  Stable Diffusion 3 with a DiT (Diffusion Transformer) architecture.
  Flux emerges as a high-quality alternative.
  Image generation becomes a commodity: more options, lower costs.

Current state of the market

The image generation market has split into two large camps:

Closed models (API):

  • DALL-E 3 (OpenAI) — native integration with GPT, consistent quality
  • Imagen (Google) — access via Vertex AI, less popular than DALL-E
  • Ideogram — strong at typography within images

Open models (open source):

  • Stable Diffusion XL, SD3 — the open source standard
  • Flux — high quality, fast growth
  • Playground v2 — oriented toward photorealistic quality

For this module we focus on DALL-E 3 and Stable Diffusion because:

  • They represent the two paradigms (closed vs open)
  • They have accessible APIs (OpenAI and Replicate respectively)
  • They cover 90% of production use cases
  • The techniques you learn are transferable to any other model

What Image Generation Is

Definition

Image generation is the process of creating new images from natural-language text descriptions (prompts). Diffusion models — the dominant architecture — work by progressively "cleaning up" noise until they form an image coherent with the prompt.

You don't need to understand the math of diffusion models to use the APIs. But you do need to understand the parameters that control generation, because they determine the quality, style, cost and time of your results.

How it works (high level)

Prompt (text) → Encoder → Latent space → Diffusion process → Decoder → Image (pixels)
  1. The prompt is converted into a numeric vector (embedding)
  2. The model starts from random noise in a compressed (latent) space
  3. At each diffusion step, the model reduces the noise guided by the prompt's embedding
  4. More steps = more detail, but more time and cost
  5. The resulting latent is decoded into an image in pixels

Main providers

ProviderModelAPI typeApprox. costMain features
OpenAIDALL-E 3Official REST API$0.04-0.08/imgHigh quality, superior semantic understanding, rewrites prompts
ReplicateSDXL, SD3, FluxREST API$0.002-0.02/imgMultiple models, pay per second of GPU
Stability AISD3, SDXLOfficial REST API$0.01-0.03/imgDirect access to the developer of Stable Diffusion
MidjourneyMidjourney v6Discord only (no REST API)$10-60/mo subscriptionExceptional artistic quality, but no programmatic API
GoogleImagen 2Vertex AIVariableIntegration with Google Cloud
Hugging FaceMultipleInference APIFree (limited)Ideal for experimenting, requires GPU for production

Production Use Cases

1. Marketing and advertising

Generate variations of ad creatives for A/B testing. A single prompt can produce dozens of images to test in campaigns. The cost of a graphic designer for 50 variations is prohibitive; with DALL-E 3 it costs less than $4.

campaign_prompts = [
    "Minimalist tech product on a white background, soft studio lighting",
    "Tech product on a wooden desk, natural window light",
    "Tech product floating against a blue-to-purple gradient background",
]

2. E-commerce

Generate product images from different angles, backgrounds or usage contexts. It eliminates the need for photo shoots for each color variation or scenario.

backgrounds = ["white studio background", "modern kitchen", "minimalist desk", "outdoor nature"]
for background in backgrounds:
    prompt = f"Stainless steel water bottle on {background}, professional product photography"

3. Prototyping and design

Generate interface mockups, visual concepts for presentations, storyboards for video. It lets you iterate fast before involving a designer for the final result.

4. Editorial content

Illustrations for articles, thumbnails for videos, images for social media. Publications that used to require generic stock photos now have unique illustrations aligned with the content.

5. Multimodal pipelines

The most advanced use case: combining generation with analysis. Real examples:

  • Document → Diagram: Analyze a PDF with GPT-4 Vision → extract structure → generate a diagram with DALL-E
  • Description → Image → Analysis: Generate an image → analyze it with vision → iterate on the prompt
  • Visual chatbot: User describes what they want → system generates → user gives feedback → system adjusts
def pipeline_document_to_diagram(document_path: str) -> str:
    text = analyze_document(document_path)
    image_prompt = f"Technical diagram showing: {text[:500]}"
    image_url = generate_image(image_prompt)
    return image_url

6. Gaming and entertainment

Asset generation: textures, backgrounds, conceptual sprites, concept art. Especially useful in early development phases where many fast iterations are needed.


Module 4 Roadmap

#CapsuleWhat you'll learnDuration
01Introduction (this one)Landscape, models, use cases, setup8 min
02DALL-E 3 (OpenAI)Full API, parameters, formats, costs8 min
03Stable DiffusionReplicate API, SDXL, advanced parameters8 min
04DALL-E vs SD ComparisonBenchmark, costs, architecture decisions7 min
05Prompts for imagesVisual prompt engineering, negative prompts7 min
06Editing and variationsInpainting, outpainting, variations6 min
07Pipeline integrationGeneration + vision, composite flows6 min
08Project: Image GeneratorDALL-E + SD fallback, complete system10 min

Estimated total module duration: ~60 min


Module Objectives

By completing this module you'll be able to:

ObjectiveCapsule where it's covered
Use the DALL-E 3 API with all its parameters (size, quality, style)02
Integrate Stable Diffusion via Replicate with fine parameter control03
Compare DALL-E vs SD and choose the right one for the context04
Design effective prompts for image generation05
Apply editing (inpainting) and generate variations06
Build pipelines that combine generation with analysis07
Implement an Image Generator with fallback between providers08

Specific technical skills

  • Calls to the OpenAI Images API (client.images.generate)
  • Calls to Replicate for Stable Diffusion
  • Handling URL and base64 responses
  • Downloading and storing generated images
  • Negative prompts for Stable Diffusion
  • Sampling parameters (steps, guidance_scale, scheduler)
  • Error handling for content policy violations
  • Fallback pattern across multiple providers

Prerequisites

What you need from earlier modules

This module assumes you completed modules 1-3 or have equivalent experience:

PrerequisiteWhere you learned it
Configuring OPENAI_API_KEY and using the OpenAI() clientModule 1
Understanding the difference between analysis and generation modelsModule 1
Working with images in base64 and URLsModule 2
Handling API errors (rate limits, content policy)Module 2
Building reusable functions that call APIsModule 3

Quick check

If you can run this code without issues, you're ready:

from openai import OpenAI

client = OpenAI()
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Respond only 'OK'"}],
    max_tokens=5
)
print(response.choices[0].message.content)

If you get OK, your API key works and you can continue.


Technical Setup

Dependencies

pip install openai>=1.0.0 replicate requests Pillow
PackageWhat we use it for
openaiDALL-E 3 API
replicateStable Diffusion API via Replicate
requestsDownloading images from URLs
PillowBasic image manipulation (resize, display)

API Keys

export OPENAI_API_KEY="sk-..."
export REPLICATE_API_TOKEN="r8_..."

OPENAI_API_KEY is required for capsules 2, 4-8. REPLICATE_API_TOKEN is required for capsules 3, 4, 7-8. You can create a free account at replicate.com that includes initial credits.

Setup verification

import openai
import replicate

print(f"openai version: {openai.__version__}")
print(f"replicate imported successfully")

client = openai.OpenAI()

response = client.images.generate(
    model="dall-e-3",
    prompt="A red square on a white background",
    size="1024x1024",
    quality="standard",
    n=1
)
print(f"DALL-E 3 works. URL: {response.data[0].url[:80]}...")

If you get a URL, your setup is complete for the whole module.

Recommended file structure

module-04-workspace/
├── generated/           ← Generated images are saved here
├── 01_intro.py
├── 02_dalle3.py
├── 03_stable_diffusion.py
├── 04_comparison.py
├── 05_prompts.py
├── 06_editing.py
├── 07_pipelines.py
└── 08_project.py

What This Module Does NOT Cover

It's equally important to know what we will not cover to avoid false expectations:

TopicWhy we don't cover itWhere to learn it
Fine-tuning image modelsRequires GPUs, datasets, and is a specialized fieldStability AI documentation, ML courses
Training from scratchOut of scope for AI Engineers (vs ML Engineers)Original papers, deep learning courses
Midjourney (Discord interface)No REST API; not integrable in codeMidjourney documentation
Video generationStill immature for production; will change fastFuture module or guide update
Advanced ControlNetRequires local GPU infrastructureStable Diffusion documentation
Legal copyright aspectsDepends on jurisdiction and changes constantlyConsult up-to-date legal sources

Mental Model: Analysis vs Generation

In modules 1-3 you worked the image → text flow (analysis). Now we work text → image (generation). This change of direction has practical implications:

AspectAnalysis (M1-3)Generation (M4)
InputExisting imageText (prompt)
OutputText (description, data)New image
DeterminismHigh (same image = same analysis)Low (same prompt ≠ same image)
ControlSpecific questionsPrompt engineering + parameters
CostBased on input/output tokensBased on resolution and quality
Typical errorsHallucinations in textVisual artifacts, malformed hands
EvaluationCompare against ground truthSubjective + defined criteria

Non-determinism is the most important difference. Every time you generate an image with the same prompt, you get a different result. This is a feature (variety) and a bug (inconsistency) at the same time. You'll learn to manage it with seeds and parameters.


Self-Assessment

Before moving on to capsule 2, check that you can answer these questions:

Conceptual:

  1. What are the two main paradigms of image generation (closed vs open)?
  2. Why is image generation non-deterministic?
  3. How does using DALL-E 3 vs Stable Diffusion differ from a developer's perspective?

Practical: 4. Do you have your OPENAI_API_KEY configured and working? 5. Do you have REPLICATE_API_TOKEN configured (or do you know how to get it)? 6. Can you name three production use cases for image generation?

Expected answers
  1. Closed: DALL-E 3 (OpenAI) — proprietary API, high quality, less control. Open: Stable Diffusion — open source, more control (negative prompts, parameters), runnable locally.
  2. Because the diffusion process starts from different random noise on each run. The seed controls that noise; same seed = same image.
  3. DALL-E 3: a single API (openai), few parameters, consistent quality, more expensive. SD: multiple APIs (Replicate, Stability, local), many parameters (steps, cfg_scale, scheduler, negative_prompt), cheaper, requires more tuning.
  4. Run the verification code from the Setup section.
  5. Sign up at replicate.com → Settings → API tokens.
  6. Marketing (creative variations), e-commerce (product photos), multimodal pipelines (document → diagram).

Evidence of Success

By the end of this module, you'll know you succeeded if:

  • ✅ You can generate an image with DALL-E 3 from a prompt and receive the result's URL or Base64
  • ✅ You can generate an image with Stable Diffusion via Replicate, configuring parameters like steps, cfg_scale and negative prompt
  • ✅ You know the differences in cost, control and quality between DALL-E 3 and Stable Diffusion
  • ✅ You know how to design effective prompts for image generation (structure, descriptors, styles)
  • ✅ You can implement a fallback that tries DALL-E 3 first and falls back to Stable Diffusion if it fails
  • ✅ Your Image Generator with Fallback works: it generates images, verifies quality, and tracks costs

Summary

  • Image generation is the modality that creates visual content from text — the complement of the analysis you mastered in M1-M3.
  • The two main paradigms are: closed (DALL-E 3 — proprietary API, high quality, less control) and open (Stable Diffusion — open source, more parameters, cheaper).
  • Real use cases include: marketing (creative variations), e-commerce (product photos), multimodal pipelines (document → diagram).
  • This module covers: the DALL-E 3 API, Stable Diffusion via Replicate, visual prompt engineering, editing/variations, and generation pipelines.
  • The module's project is an Image Generator with Fallback that tries multiple providers, verifies quality and tracks costs.

Module Terminology

TermMeaning
PromptText that describes the image to generate
Negative promptText that describes what should NOT appear (Stable Diffusion only)
SeedNumber that controls the initial noise; same seed = same result
StepsNumber of diffusion steps; more steps = more detail
CFG Scale / GuidanceHow strictly the model follows the prompt (higher = more literal)
InpaintingEditing a specific area of an existing image
OutpaintingExpanding an image beyond its original borders
VariationsGenerating images similar to a reference image
Revised promptThe prompt rewritten internally by DALL-E 3
SchedulerThe sampling algorithm in Stable Diffusion (K_EULER, DPM, etc.)

Additional Resources

  1. OpenAI Images API — Official DALL-E 3 documentation
  2. Replicate — Stable Diffusion and other models via API
  3. Stability AI — Creators of Stable Diffusion
  4. Prompt Engineering for Image Generation (OpenAI Cookbook) — Advanced examples
  5. Hugging Face Diffusers — Library for running diffusion models locally