Elevate Your Projects with the Image Generation API

10 May 2025
Elevate Your Projects with the Image Generation API

The Image Generation and Prediction API offers developers an innovative way to create custom images based on text prompts and image inpainting techniques. This powerful tool allows for high levels of customization, enabling users to generate visually compelling content quickly and efficiently. By leveraging advanced models and various configuration settings, developers can enhance their applications with unique imagery tailored to specific needs.

Imagine being able to generate stunning visuals for marketing campaigns, social media content, or even personalized artwork with just a few lines of code. With the Image Generation and Prediction API, the possibilities are endless. Whether you're looking to create concept art, product mockups, or engaging storytelling visuals, this API simplifies the image generation process, saving you time while maximizing creative potential.

Prerequisites

Before getting started, ensure you have a Cognitive Actions API key and a basic understanding of making API calls. This will allow you to seamlessly integrate the API into your applications.

Generate Image with Inpainting and LoRA Weights

This action enables developers to create custom images by using text prompts in conjunction with image inpainting and LoRA weights. It solves the challenge of producing tailored images while allowing for various optimizations such as aspect ratio, output quality, and guidance scale to refine the results.

Input Requirements

The input for this action is a structured object that requires a prompt, while also allowing for optional parameters such as mask, image, model, width, height, and more. For example:

  • Prompt: A descriptive text that guides the image generation.
  • Image: An optional URI of an input image for inpainting.
  • Mask: An optional URI of the image mask for inpainting mode.
  • Model: Choose between 'dev' or 'schnell' for optimized predictions.

Expected Output

The output consists of one or more generated images, depending on the outputCount specified. Each image is returned as a URI link pointing to the generated image.

Use Cases for this Specific Action

  • Marketing Content: Generate eye-catching visuals for advertisements or social media posts that resonate with target audiences.
  • Prototyping: Quickly create mockups for products or concepts that require visual representation.
  • Artistic Projects: Enable artists and designers to experiment with new styles and concepts through AI-generated imagery.
  • Game Development: Generate unique character designs or backgrounds that enhance the gaming experience.

```python
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 = "8c9df271-4be6-470c-b722-fe528f3cd3f6" # Action ID for: Generate Image with Inpainting and LoRA Weights

# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
  "model": "dev",
  "prompt": "make young ginger boy TIKTOAL look like the new winner of Nobel Price in Hollywood. He is happy, many people are applauding him. He is on the red carpet in Walk of Fame.",
  "megapixels": "1",
  "outputCount": 4,
  "loraStrength": 1,
  "guidanceScale": 3,
  "outputQuality": 80,
  "extraLoraScale": 1,
  "promptStrength": 0.8,
  "imageAspectRatio": "1:1",
  "optimizeForSpeed": false,
  "outputImageFormat": "png",
  "denoisingStepsCount": 47
}

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 Prediction API is an invaluable tool for developers looking to enhance their applications with custom imagery. By utilizing its powerful features, you can streamline the image creation process, allowing for rapid prototyping and creative exploration. Whether for marketing, artistic endeavors, or game development, this API opens up new possibilities for visual storytelling. Start integrating it into your projects today and unlock the full potential of AI-driven image generation!