Enhance Your App with Image Inpainting Using the Leonardo A2 Cognitive Actions

22 Apr 2025
Enhance Your App with Image Inpainting Using the Leonardo A2 Cognitive Actions

In the world of digital creativity, generating high-quality images can often be a challenge, especially when it involves filling in missing or masked areas. The Leonardo A2 Cognitive Actions provide developers with powerful tools to perform image inpainting effectively. These pre-built actions enable seamless integration of advanced image generation capabilities into your applications, allowing for unique and artistic visual content creation.

Prerequisites

Before diving into the integration of the Cognitive Actions, ensure you have the following ready:

  • An API key for the Cognitive Actions platform to authenticate your requests.
  • A basic understanding of making HTTP requests and handling JSON data.
  • Familiarity with Python for implementing the conceptual code examples provided.

Authentication typically involves passing your API key in the request headers, which helps secure your interactions with the Cognitive Actions services.

Cognitive Actions Overview

Generate Inpainted Image

The Generate Inpainted Image action utilizes sophisticated image inpainting techniques to create high-quality images by filling in the specified missing or masked areas. This action offers flexibility by allowing the use of different models for varying inference speeds.

Input

The input for this action requires a structured JSON object containing various parameters. Below is a breakdown of the expected input schema along with an example:

  • prompt (string, required): Text prompt guiding the image generation.
  • image (string, optional): Input image for inpainting mode.
  • mask (string, optional): Image mask for inpainting.
  • model (string, optional): Model for inference, default is "dev".
  • aspectRatio (string, optional): Aspect ratio of the generated image.
  • numberOfOutputs (integer, optional): Number of output images to generate.
  • outputFormat (string, optional): Format of the output images.

Example Input:

{
  "image": "https://replicate.delivery/pbxt/LxxoENxmOyzYyWnqd1hdlyXpj9ClWtodW7HlaaX3hIl5uc86/af6af8185bc1b89e306e9b463f31926d.jpg",
  "model": "dev",
  "prompt": "A football stadium terrace, dimly lit and filled with shadows...",
  "loraScale": 1,
  "aspectRatio": "16:9",
  "outputFormat": "jpg",
  "guidanceScale": 3.5,
  "outputQuality": 90,
  "extraLoraScale": 1,
  "promptStrength": 0.8,
  "numberOfOutputs": 3,
  "numberOfInferenceSteps": 50
}

Output

The expected output from the Generate Inpainted Image action consists of an array of URLs pointing to the generated images. Each URL corresponds to a unique output based on the provided prompt and parameters.

Example Output:

[
  "https://assets.cognitiveactions.com/invocations/3d49f504-df0a-4924-9329-2045ac8a63ee/9ac4861a-9e78-4c9c-8334-e5911fd8787a.jpg",
  "https://assets.cognitiveactions.com/invocations/3d49f504-df0a-4924-9329-2045ac8a63ee/23f66b13-973d-4b30-9aa1-f14a57f5af47.jpg",
  "https://assets.cognitiveactions.com/invocations/3d49f504-df0a-4924-9329-2045ac8a63ee/fd791ae4-07af-442b-8213-44f67ef7d9dd.jpg"
]

Conceptual Usage Example (Python)

Below is a conceptual example of how to invoke the Generate Inpainted Image action using Python. This example assumes you have the necessary API key and endpoint URL:

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 = "22bf39b6-5ec7-492a-8453-3321c9c1a76e"  # Action ID for Generate Inpainted Image

# Construct the input payload based on the action's requirements
payload = {
    "image": "https://replicate.delivery/pbxt/LxxoENxmOyzYyWnqd1hdlyXpj9ClWtodW7HlaaX3hIl5uc86/af6af8185bc1b89e306e9b463f31926d.jpg",
    "model": "dev",
    "prompt": "A football stadium terrace, dimly lit and filled with shadows...",
    "loraScale": 1,
    "aspectRatio": "16:9",
    "outputFormat": "jpg",
    "guidanceScale": 3.5,
    "outputQuality": 90,
    "extraLoraScale": 1,
    "promptStrength": 0.8,
    "numberOfOutputs": 3,
    "numberOfInferenceSteps": 50
}

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 code snippet, you will need to replace the placeholder with your actual API key and ensure the endpoint is correctly specified. The payload is constructed based on the required parameters of the action.

Conclusion

Integrating the Generate Inpainted Image action from the Leonardo A2 Cognitive Actions can significantly enhance the creative capabilities of your application. By leveraging the power of image inpainting, developers can deliver unique and high-quality visual content tailored to their users' needs. Explore the possibilities of dynamic image generation and take your app to the next level!