Enhance Your Applications with Image Generation: Integrating the SDXL Paintings Cognitive Actions

23 Apr 2025
Enhance Your Applications with Image Generation: Integrating the SDXL Paintings Cognitive Actions

In today's digital landscape, the ability to generate and manipulate images programmatically can open up a world of creative possibilities for developers. The SDXL Paintings Cognitive Actions enable you to harness advanced image processing capabilities, such as inpainting, through a simple API. This blog post will guide you through using the Generate Inpainted Image action, allowing you to create stunning visuals while preserving specific areas of your images.

Prerequisites

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

  • An API key for the Cognitive Actions platform.
  • Basic knowledge of JSON and RESTful API concepts.
  • Python environment set up to execute the example code snippets.

Authentication typically involves passing your API key in the request headers, enabling secure access to the Cognitive Actions.

Cognitive Actions Overview

Generate Inpainted Image

The Generate Inpainted Image action allows you to create an inpainted image using an input mask and other parameters. This process preserves specified areas of the image while generating new content in the masked areas, leveraging the powerful SDXL Paintings model to enhance the quality of the generated visuals.

Input

The action requires a JSON object structured as follows:

  • mask (string, required): URI of the input mask for inpainting. Black areas will be preserved, while white areas will be inpainted.
  • seed (integer, optional): Random seed for deterministic results. Leave blank for a random seed.
  • image (string, required): URI of the input image used in the transformation.
  • width (integer, default: 1024): Width of the output image in pixels.
  • height (integer, default: 1024): Height of the output image in pixels.
  • prompt (string, default: "An astronaut riding a rainbow unicorn"): Text prompt guiding the image generation.
  • loraScale (number, default: 0.6): Scale factor for LoRA model adjustments, applicable only on trained models.
  • loraWeights (string, optional): Specify LoRA weights; leave blank to use defaults.
  • refineStyle (string, default: "no_refiner"): Select the refinement style from available options.
  • guidanceScale (number, default: 7.5): Factor for classifier-free guidance, ranging from 1 to 50.
  • applyWatermark (boolean, default: true): Enable or disable watermark on generated images.
  • denoisingSteps (integer, default: 50): Number of steps for denoising.
  • negativePrompt (string, optional): Prompt to exclude certain aspects from the generation.
  • promptStrength (number, default: 0.8): Strength of the prompt during transformation.
  • refinementSteps (integer, optional): Number of refinement steps for 'base_image_refiner'.
  • outputImageCount (integer, default: 1): Number of images to generate (1-4).
  • schedulingMethod (string, default: "K_EULER"): Scheduling algorithm for image generation.
  • highNoiseFraction (number, default: 0.8): Fraction of noise used in 'expert_ensemble_refiner'.
  • safetyCheckerDisabled (boolean, default: false): Disable the safety check for generated images.

Example Input:

{
  "width": 1024,
  "height": 1024,
  "prompt": "In the style of TOK, a sunset over the Hudson River from West Village in NYC",
  "loraScale": 0.6,
  "refineStyle": "no_refiner",
  "guidanceScale": 7.5,
  "applyWatermark": true,
  "denoisingSteps": 50,
  "negativePrompt": "",
  "promptStrength": 0.8,
  "outputImageCount": 1,
  "schedulingMethod": "K_EULER",
  "highNoiseFraction": 0.8
}

Output

Upon successful execution, the action typically returns a list containing the URI of the generated image.

Example Output:

[
  "https://assets.cognitiveactions.com/invocations/3df653c3-1cd2-433e-bbb6-945f994d47c8/343c442e-53c3-4301-b12a-15d4bdceb375.png"
]

Conceptual Usage Example (Python)

Here is a conceptual Python code snippet demonstrating how to call the Generate Inpainted Image action:

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 = "ea82154d-a52a-4bb3-b409-8c9de1ed404f" # Action ID for Generate Inpainted Image

# Construct the input payload based on the action's requirements
payload = {
    "width": 1024,
    "height": 1024,
    "prompt": "In the style of TOK, a sunset over the Hudson River from West Village in NYC",
    "loraScale": 0.6,
    "refineStyle": "no_refiner",
    "guidanceScale": 7.5,
    "applyWatermark": True,
    "denoisingSteps": 50,
    "negativePrompt": "",
    "promptStrength": 0.8,
    "outputImageCount": 1,
    "schedulingMethod": "K_EULER",
    "highNoiseFraction": 0.8
}

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}
    )
    response.raise_for_status()

    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, replace YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key. The input payload is structured according to the action's requirements, and the response is printed in a formatted JSON style.

Conclusion

The SDXL Paintings Cognitive Actions provide developers with powerful tools to manipulate and generate images effortlessly. By integrating the Generate Inpainted Image action into your applications, you can create visually compelling content that meets your specific needs.

Explore further by experimenting with different parameters and combining actions to unlock even more creative potential! Happy coding!