{
  "name": "Image Generator Workflow",
  "nodes": [
    {
      "parameters": {},
      "id": "workflow-start",
      "name": "Start",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "// Calculate number of images needed\nconst data = $input.item.json;\nconst duration = data.estimatedDuration || 60;\nconst imagesPerMinute = data.imagesPerMinute || 3;\nconst numImages = Math.ceil((duration / 60) * imagesPerMinute);\n\nconst apiKey = data.apiKeys?.imageApi || $env.IMAGE_API_KEY;\n\nreturn {\n  apiKey,\n  numImages,\n  script: data.script,\n  topic: data.topic,\n  userId: data.userId,\n  imagePrompts: [] // Will be filled in next node\n};"
      },
      "id": "calculate-images",
      "name": "Calculate Image Count",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "// Generate image prompts from script\nconst data = $input.item.json;\nconst script = data.script;\nconst numImages = data.numImages;\n\n// Split script into segments\nconst words = script.split(' ');\nconst wordsPerImage = Math.floor(words.length / numImages);\n\nconst prompts = [];\nfor (let i = 0; i < numImages; i++) {\n  const start = i * wordsPerImage;\n  const segment = words.slice(start, start + wordsPerImage).join(' ');\n  \n  // Create visual prompt from text segment\n  const prompt = `Create a professional, high-quality image representing: ${segment.substring(0, 200)}. Style: modern, clean, vibrant colors.`;\n  \n  prompts.push({\n    index: i,\n    prompt: prompt.substring(0, 300) // Limit prompt length\n  });\n}\n\nreturn {\n  apiKey: data.apiKey,\n  imagePrompts: prompts,\n  topic: data.topic,\n  userId: data.userId\n};"
      },
      "id": "create-prompts",
      "name": "Create Image Prompts",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "id": "split-prompts",
      "name": "Split Prompts",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.stability.ai/v1/generation/stable-diffusion-v1-6/text-to-image",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $('Calculate Image Count').item.json.apiKey }}"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "text_prompts",
              "value": "=[{\"text\": \"{{ $json.prompt }}\", \"weight\": 1}]"
            },
            {
              "name": "cfg_scale",
              "value": "7"
            },
            {
              "name": "height",
              "value": "720"
            },
            {
              "name": "width",
              "value": "1280"
            },
            {
              "name": "samples",
              "value": "1"
            }
          ]
        },
        "options": {}
      },
      "id": "generate-image",
      "name": "Generate Image",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1050,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "// Process generated image\nconst response = $input.item.json;\nconst imageBase64 = response.artifacts[0].base64;\nconst index = $('Split Prompts').item.json.index;\n\nreturn {\n  imageIndex: index,\n  imageBase64: imageBase64,\n  fileName: `image_${index}_${Date.now()}.png`,\n  mimeType: 'image/png'\n};"
      },
      "id": "process-image",
      "name": "Process Image",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1250,
        300
      ]
    },
    {
      "parameters": {
        "amount": 2,
        "unit": "seconds"
      },
      "id": "rate-limit",
      "name": "Rate Limit Wait",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [
        1450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "// Aggregate all images\nconst allImages = $input.all();\n\nreturn {\n  images: allImages.map(img => ({\n    index: img.json.imageIndex,\n    base64: img.json.imageBase64,\n    fileName: img.json.fileName,\n    mimeType: img.json.mimeType\n  })),\n  topic: $('Calculate Image Count').item.json.topic,\n  userId: $('Calculate Image Count').item.json.userId\n};"
      },
      "id": "aggregate-images",
      "name": "Aggregate Images",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1650,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Calculate Image Count",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Image Count": {
      "main": [
        [
          {
            "node": "Create Image Prompts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Image Prompts": {
      "main": [
        [
          {
            "node": "Split Prompts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Prompts": {
      "main": [
        [
          {
            "node": "Generate Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Image": {
      "main": [
        [
          {
            "node": "Process Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Image": {
      "main": [
        [
          {
            "node": "Rate Limit Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Rate Limit Wait": {
      "main": [
        [
          {
            "node": "Split Prompts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 0,
  "updatedAt": "2026-01-25T00:00:00.000Z",
  "versionId": "1"
}