Create Stunning Images with the Image Generation and Inpainting API

11 May 2025
Create Stunning Images with the Image Generation and Inpainting API

The Image Generation and Inpainting API offers developers a powerful tool for creating and modifying images through advanced inpainting techniques. This API not only speeds up the image creation process but also simplifies the integration of complex image generation tasks into applications. With the ability to customize prompts and control various parameters, developers can achieve tailored outputs that meet their specific needs.

Common use cases for this API include creating unique artwork, enhancing existing images, and generating content for games or virtual environments. Whether you're looking to fill in missing parts of an image or generate entirely new visuals, this API provides an efficient and effective solution.

Prerequisites

To get started, you'll need an API key for the Cognitive Actions service and a basic understanding of making API calls.

Generate Image with Inpainting

This operation allows for image generation using inpainting techniques. It supports several models, including 'ponyRealism_V23', 'tPonynai3_v7', and 'waiANINSFWPONYXL_v140'. Developers can customize prompts using Compel weighting syntax and have control over generation steps, noise levels, image dimensions, and more for tailored output.

Input Requirements

The input for this action is a composite request that includes various parameters:

  • Mask: URI of the mask image for inpainting, where white areas will be modified, and black areas will be preserved.
  • Seed: An integer value for random generation; set to -1 for a random seed.
  • Image: URI of the base image for image-to-image tasks.
  • Model: Choose from supported models for image generation.
  • Steps: The number of steps to perform during generation (1 to 100).
  • Width & Height: Dimensions of the output image.
  • Prompt: A descriptive prompt for the image, utilizing Compel weighting syntax.
  • Strength: Degree of noise to add (0 to 1), applicable for inpainting.
  • Scheduler: The scheduling method used during generation.
  • Clip Layer Skip: Number of CLIP layers to omit.
  • Excluded Prompt: A negative prompt specifying elements to avoid.
  • Number of Images: Batch size for image generation (1-4).
  • Noise Rescale Factor: Rescale factor for generated noise.
  • Add Initial Preprompt: Option to prepend a predefined preprompt.
  • Inpainting Blur Factor: Level of blur applied to the inpainting mask.
  • Variation Autoencoder: VAE model for image encoding and decoding.
  • Configuration Guidance Scale: Attention level given to the prompt.
  • Progressive Attention Guidance Scale: Quality-affecting scale similar to CFG.
  • Layered Operations and Rational Scales: Specifies LoRAs used in the operation.

Expected Output

The expected output is a URI of the generated image. For example:

  • https://assets.cognitiveactions.com/invocations/b4432bf2-b0e2-4dad-8792-98a6c11e75c3/80c5e02b-c924-4b21-873c-7fda3e3dd768.png

Use Cases for this Specific Action

This action is particularly useful for:

  • Art and Design: Artists can create unique pieces or modify existing work seamlessly.
  • Game Development: Generate assets for characters, environments, or textures dynamically.
  • Marketing and Content Creation: Produce high-quality visuals for advertisements or social media without the need for extensive graphic design resources.
import requests
import json

# Replace with your actual Cognitive Actions API key and endpoint
# Ensure your environment securely handles the API key
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
# This endpoint URL is hypothetical and should be documented for users
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"

action_id = "3750ece6-edd2-48db-9362-6502b8d5061f" # Action ID for: Generate Image with Inpainting

# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
  "seed": -1,
  "model": "tPonynai3_v7",
  "steps": 35,
  "width": 1184,
  "height": 864,
  "prompt": "1girl",
  "strength": 0.7,
  "scheduler": "Euler a",
  "clipLayerSkip": 1,
  "excludedPrompt": "animal, cat, dog, big breasts",
  "numberOfImages": 1,
  "noiseRescaleFactor": 0.5,
  "addInitialPreprompt": true,
  "inpaintingBlurFactor": 5,
  "variationAutoencoder": "default",
  "configurationGuidanceScale": 5,
  "progressiveAttentionGuidanceScale": 3
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json",
    # Add any other required headers for the Cognitive Actions API
}

# Prepare the request body for the hypothetical execution endpoint
request_body = {
    "action_id": action_id,
    "inputs": payload
}

print(f"--- Calling Cognitive Action: {action.name or action_id} ---")
print(f"Endpoint: {COGNITIVE_ACTIONS_EXECUTE_URL}")
print(f"Action ID: {action_id}")
print("Payload being sent:")
print(json.dumps(request_body, indent=2))
print("------------------------------------------------")

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json=request_body
    )
    response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully. Result:")
    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 (non-JSON): {e.response.text}")
    print("------------------------------------------------")

Conclusion

The Image Generation and Inpainting API provides developers with a versatile tool for creating and enhancing images through advanced inpainting techniques. With customizable parameters and support for various models, this API can significantly streamline the image generation process.

To explore the full potential of this API, start integrating it into your projects today and unlock new possibilities for creative visual content!