Generate Stunning Images with Cognitive Actions for PixelPunks

24 Apr 2025
Generate Stunning Images with Cognitive Actions for PixelPunks

In the world of creative applications, generating images based on specific prompts has become increasingly popular. The Codelace PixelPunks Cognitive Actions empower developers to seamlessly integrate image generation capabilities into their applications. With features like customizable resolutions, aspect ratios, and enhanced models for speed and quality, these actions provide a robust solution for generating unique images on demand.

Prerequisites

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

  • An API key for the Cognitive Actions platform.
  • Familiarity with JSON payloads and HTTP requests.

For authentication, you will typically pass the API key in the headers of your requests, which allows you to access the available actions securely.

Cognitive Actions Overview

Generate Image with Inpainting

The Generate Image with Inpainting action allows you to create an image based on a specified prompt and, if desired, a mask for inpainting. This action supports various models that trade off between speed and quality, letting you customize image resolutions, aspect ratios, and more. You can generate between 1 to 4 images in formats like JPG, PNG, and WEBP.

Input

The required input for this action includes several properties:

  • prompt (string, required): The main prompt for generating the image. Including specific trigger words can enhance the likelihood of achieving the desired output.
  • model (string, optional): Choose between "dev" and "schnell" models based on your requirements for speed and quality (default is "dev").
  • numOutputs (integer, optional): Specifies the number of images to generate (default is 1, maximum is 4).
  • aspectRatio (string, optional): Defines the aspect ratio of the image (default is "1:1").
  • outputFormat (string, optional): Select the output format of the images (default is "webp").
  • Additional optional parameters include mask, seed, width, height, goFast, guidanceScale, and more.

Here’s an example of the input JSON payload:

{
  "model": "schnell",
  "goFast": false,
  "prompt": "TOK",
  "loraScale": 1,
  "megapixels": "1",
  "numOutputs": 1,
  "aspectRatio": "1:1",
  "outputFormat": "png",
  "guidanceScale": 0.26,
  "outputQuality": 80,
  "extraLoraScale": 1,
  "promptStrength": 0,
  "numInferenceSteps": 4
}

Output

The action typically returns an array of URLs pointing to the generated images. For instance, a successful response might look like this:

[
  "https://assets.cognitiveactions.com/invocations/1a7c9ad7-37e5-4999-b35e-ac5e30be6d72/67bc8171-8ad9-4c3b-9131-0c49e3b5593a.png"
]

Conceptual Usage Example (Python)

Here’s how you could invoke the Generate Image with Inpainting action using Python:

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 = "257d1123-9bac-452e-8d3a-056c4a10d187"  # Action ID for Generate Image with Inpainting

# Construct the input payload based on the action's requirements
payload = {
    "model": "schnell",
    "goFast": False,
    "prompt": "TOK",
    "loraScale": 1,
    "megapixels": "1",
    "numOutputs": 1,
    "aspectRatio": "1:1",
    "outputFormat": "png",
    "guidanceScale": 0.26,
    "outputQuality": 80,
    "extraLoraScale": 1,
    "promptStrength": 0,
    "numInferenceSteps": 4
}

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 example, you replace the placeholder with your actual API key and endpoint. The action ID and input payload structure should align with the action's requirements.

Conclusion

The Codelace PixelPunks Cognitive Actions provide a powerful tool for developers aiming to generate images dynamically based on user input. By leveraging the capabilities of the Generate Image with Inpainting action, you can enhance your applications with rich visual content, offering users a unique and engaging experience. Explore various prompts, models, and configurations to find the perfect fit for your project's needs!