Enhance Your Applications with Image Generation Using Cognitive Actions

In today's digital landscape, the ability to create and manipulate images programmatically can greatly enhance applications across various domains. The jakobsitter/js_sdxl API provides powerful Cognitive Actions for image generation, enabling developers to create and modify images using advanced techniques like inpainting. With these pre-built actions, you can quickly integrate sophisticated image generation capabilities into your applications, offering users unique visual experiences.
Prerequisites
Before diving into the Cognitive Actions, ensure you have the following:
- An API key for the Cognitive Actions platform to authenticate your requests.
- Basic familiarity with making HTTP requests and handling JSON data.
Authentication typically involves including the API key in the headers of your requests, allowing secure access to the image generation features.
Cognitive Actions Overview
Generate Image with Inpainting
The Generate Image with Inpainting action allows you to create and modify images based on textual prompts, offering a variety of options for customization. This action is particularly useful for generating images that require specific alterations, guided by user-defined parameters.
Input
The input for this action follows the CompositeRequest schema, which includes several fields to tailor the image generation process:
- mask (string, URI): The input mask for inpainting. Black areas are preserved, while white areas are modified.
- seed (integer, optional): Random seed for image generation. If left blank, a random seed is automatically generated.
- image (string, URI): The input image for img2img and inpainting modes.
- width (integer, default: 1024): Desired output image width.
- height (integer, default: 1024): Desired output image height.
- prompt (string, default: "An astronaut riding a rainbow unicorn"): Text input guiding the image generation process.
- refine (string, enum: "no_refiner", "expert_ensemble_refiner", "base_image_refiner", default: "no_refiner"): The style of refinement to apply.
- scheduler (string, enum: various options, default: "K_EULER"): Scheduler type for the inference process.
- antiPrompt (string): Negative prompt to influence the generation.
- numberOfOutputs (integer, default: 1, max: 4): Number of images to generate.
Here’s an example input JSON payload for the action:
{
"width": 1024,
"height": 1024,
"prompt": "a photo of TOK in a big pool, drowning",
"refine": "no_refiner",
"scheduler": "K_EULER",
"applyWatermark": true,
"denoisingSteps": 50,
"numberOfOutputs": 1,
"highNoiseFraction": 0.8,
"loraAdditiveScale": 0.6,
"initialPromptStrength": 0.8,
"classifierGuidanceScale": 7.5
}
Output
Upon successful execution, the action typically returns a list of generated image URLs. Here’s an example output:
[
"https://assets.cognitiveactions.com/invocations/70c51ccf-10c8-4936-a687-07b19d10e466/d33b27d2-28a6-42ad-b46b-f54eb33f30fa.png"
]
Conceptual Usage Example (Python)
To execute the Generate Image with Inpainting action, you can use the following Python code snippet. This example demonstrates how to structure the input JSON payload and make a request to the hypothetical endpoint:
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 = "3d49999d-da9e-49b9-b8cc-dd74c581f613" # Action ID for Generate Image with Inpainting
# Construct the input payload based on the action's requirements
payload = {
"width": 1024,
"height": 1024,
"prompt": "a photo of TOK in a big pool, drowning",
"refine": "no_refiner",
"scheduler": "K_EULER",
"applyWatermark": true,
"denoisingSteps": 50,
"numberOfOutputs": 1,
"highNoiseFraction": 0.8,
"loraAdditiveScale": 0.6,
"initialPromptStrength": 0.8,
"classifierGuidanceScale": 7.5
}
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 snippet, replace the COGNITIVE_ACTIONS_API_KEY and COGNITIVE_ACTIONS_EXECUTE_URL with your actual API key and endpoint. The action ID and input payload are structured according to the requirements of the Generate Image with Inpainting action.
Conclusion
The jakobsitter/js_sdxl Cognitive Actions provide a robust set of tools for developers looking to integrate image generation capabilities into their applications. By harnessing the power of inpainting and customizable parameters, you can create unique and tailored visual content that enhances user engagement. Explore these actions further to unlock new creative possibilities for your projects!