Generate Stunning Images with the Leonardo A2 Cognitive Actions

24 Apr 2025
Generate Stunning Images with the Leonardo A2 Cognitive Actions

Integrating advanced image generation capabilities into your applications is now easier than ever with the Leonardo A2 Cognitive Actions. This API offers a powerful action for generating images using inpainting techniques, allowing developers to create unique and customized visuals based on specific prompts. By leveraging pre-built capabilities, you can save time and enhance your applications with stunning imagery.

Prerequisites

Before diving into the integration, ensure you have the following:

  • An API key for the Cognitive Actions platform to authenticate your requests.
  • Basic knowledge of JSON payloads and HTTP requests.

Authentication typically involves passing your API key in the headers of your requests, which enables you to securely interact with the Cognitive Actions.

Cognitive Actions Overview

Generate Image with Inpainting

The Generate Image with Inpainting action creates images based on a provided prompt. This powerful tool utilizes different models to optimize performance for various use cases. You can customize the output with parameters such as image masks, aspect ratio, and output quality, making it a versatile choice for developers looking to create tailored images.

Input

The input schema for this action is as follows:

{
  "prompt": "string (required)",
  "mask": "string (optional, uri)",
  "image": "string (optional, uri)",
  "model": "string (default: 'dev', options: ['dev', 'schnell'])",
  "width": "integer (optional, between 256 and 1440)",
  "height": "integer (optional, between 256 and 1440)",
  "goFast": "boolean (default: false)",
  "imageFormat": "string (default: 'webp', options: ['webp', 'jpg', 'png'])",
  "outputCount": "integer (default: 1, between 1 and 4)",
  "imageQuality": "integer (default: 80, between 0 and 100)",
  "customWeights": "string (optional)",
  "loraIntensity": "number (default: 1, between -1 and 3)",
  "additionalLoraIntensity": "number (default: 1, between -1 and 3)",
  "imageMegapixels": "string (default: '1', options: ['1', '0.25'])",
  "promptIntensity": "number (default: 0.8, between 0 and 1)",
  "imageAspectRatio": "string (default: '1:1', options: ['1:1', '16:9', ...])",
  "guidanceIntensity": "number (default: 3, between 0 and 10)",
  "inferenceStepCount": "integer (default: 28, between 1 and 50)",
  "safetyCheckerDisabled": "boolean (default: false)"
}

Here’s a practical example of the JSON payload you would use:

{
  "model": "dev",
  "prompt": "Create an image of a jungle scene within a winter garden, viewed through a corridor...",
  "imageFormat": "webp",
  "outputCount": 2,
  "imageQuality": 90,
  "loraIntensity": 1,
  "promptIntensity": 0.8,
  "imageAspectRatio": "1:1",
  "guidanceIntensity": 3.5,
  "inferenceStepCount": 28,
  "additionalLoraIntensity": 1
}

Output

The output of this action typically consists of URLs pointing to the generated images. For example:

[
  "https://assets.cognitiveactions.com/invocations/70321d77-26af-4a6d-bb96-854b28ca6b41/fd1b215c-e2f7-4c51-b7cd-80b33b96b2e6.webp",
  "https://assets.cognitiveactions.com/invocations/70321d77-26af-4a6d-bb96-854b28ca6b41/bbc85578-d52b-433f-bdb1-63cc4060e54b.webp"
]

Conceptual Usage Example (Python)

Here’s a brief Python code snippet illustrating how to call this action using a hypothetical Cognitive Actions API endpoint:

import requests
import json

# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"  # Hypothetical endpoint

action_id = "e8e3b712-6774-4e22-9d1d-2cd110afdf73"  # Action ID for Generate Image with Inpainting

# Construct the input payload based on the action's requirements
payload = {
    "model": "dev",
    "prompt": "Create an image of a jungle scene within a winter garden, viewed through a corridor...",
    "imageFormat": "webp",
    "outputCount": 2,
    "imageQuality": 90,
    "loraIntensity": 1,
    "promptIntensity": 0.8,
    "imageAspectRatio": "1:1",
    "guidanceIntensity": 3.5,
    "inferenceStepCount": 28,
    "additionalLoraIntensity": 1
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json"
}

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json={"action_id": action_id, "inputs": payload}  # Hypothetical structure
    )
    response.raise_for_status()  # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully:")
    print(json.dumps(result, indent=2))

except requests.exceptions.RequestException as e:
    print(f"Error executing action {action_id}: {e}")
    if e.response is not None:
        print(f"Response status: {e.response.status_code}")
        try:
            print(f"Response body: {e.response.json()}")
        except json.JSONDecodeError:
            print(f"Response body: {e.response.text}")

In this snippet, the payload is constructed using the required fields, and a POST request is sent to execute the action. The response is then processed to display the generated image URLs.

Conclusion

The Leonardo A2 Cognitive Actions offer a powerful way to generate unique images tailored to your specifications. By leveraging the capabilities of the Generate Image with Inpainting action, you can create stunning visuals that enhance the user experience of your application. Consider exploring additional parameters to optimize your image generation further, and start integrating this functionality into your projects today!