Create Stunning Images with the Image Generation API

The Image Generation And Inpainting API provides developers with powerful tools to create and manipulate images through advanced AI-driven techniques. This API allows for the generation of custom images using image-to-image or inpainting modes, enabling a wide range of creative possibilities. By leveraging the capabilities of LoRA weights, developers can achieve high adaptability for stylized outputs and optimized performance, making it easier than ever to bring unique visual ideas to life.
Common use cases for this API include creating artwork for digital media, generating images for marketing materials, enhancing existing images with inpainting, or even developing custom avatars for games and applications. Whether you’re an artist looking to experiment with new styles or a developer needing to automate image creation, this API simplifies the process and accelerates your workflow.
Prerequisites
To get started, you'll need an API key for the Cognitive Actions service, along with a basic understanding of how to make API calls.
Generate Custom Images
This action allows users to create custom images tailored to specific prompts, using both image-to-image transformations and inpainting techniques. It is particularly useful for generating unique visuals based on user-defined criteria.
Purpose
The "Generate Custom Images" action solves the problem of needing unique and high-quality images without the extensive manual effort typically involved in graphic design. It caters to diverse creative needs, from simple graphics to complex artistic pieces.
Input Requirements
The action requires a structured input in the form of a JSON object that includes:
- prompt: A descriptive text that guides the image generation process.
- image: (optional) A URI of the input image for inpainting or transformation.
- mask: (optional) A URI for the image mask used in inpainting.
- model: Specifies which model to use for inference (e.g., "dev" or "schnell").
- width and height: Dimensions for the generated image, if a custom aspect ratio is chosen.
- Additional parameters for fine-tuning the output, like imageOutputFormat, outputCount, and inferenceStepCount.
Example Input:
{
"image": "https://replicate.delivery/pbxt/Mzjbu96kECihU6wu4YwE97EzSIBDnQxCAT8ZAQa9CTrRWUwz/IMG_3589.png",
"model": "dev",
"goFast": false,
"prompt": "bespoke, 24x24 pixel grid portrait, symbolic punk style, vibrant kiwi green solid background...",
"outputCount": 1,
"mainLoraScale": 1,
"imageMegapixels": "1",
"imageAspectRatio": "1:1",
"imageOutputFormat": "webp",
"imageOutputQuality": 80,
"inferenceStepCount": 28,
"additionalLoraScale": 1,
"imagePromptStrength": 0.8,
"diffusionGuidanceScale": 3
}
Expected Output
The output will be a URI pointing to the generated image, which can be accessed and utilized in various applications.
Example Output:
[
"https://assets.cognitiveactions.com/invocations/961dea4d-3b2c-4b3d-aa14-035ad7c3fda5/ae5cc7ea-b8dc-4eb5-be1b-3a76442993f2.webp"
]
Use Cases for this Specific Action
- Art Creation: Artists can generate unique pieces of artwork based on specific styles or themes.
- Marketing Materials: Marketers can quickly create visuals for campaigns without needing a graphic designer.
- Game Development: Developers can produce character designs or backgrounds tailored to the game’s aesthetic.
- Image Enhancement: Users can refine existing images by filling in missing parts or altering specific sections based on their requirements.
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 = "fdb0194b-f489-4d01-ae68-259094dc2acf" # Action ID for: Generate Custom Images
# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
"image": "https://replicate.delivery/pbxt/Mzjbu96kECihU6wu4YwE97EzSIBDnQxCAT8ZAQa9CTrRWUwz/IMG_3589.png",
"model": "dev",
"goFast": false,
"prompt": "bespoke, 24x24 pixel grid portrait, symbolic punk style, vibrant kiwi green solid background, white/gray hair with texture starting at y=3 extending to y=17, black pupils fixed at (8,12) and (13,12), dark iris eyes with white highlights at (9,12) and (14,12), black nose dot at (11,13), gray lips spanning x=10-13 y=15-16, light skin tone with jaw defined at y=19, dark collar at bottom edge y=20-22, right-facing, pure pixel art with no gradients or anti-aliasing, clean color boundaries, all features contained within 22x22 center grid",
"outputCount": 1,
"mainLoraScale": 1,
"imageMegapixels": "1",
"imageAspectRatio": "1:1",
"imageOutputFormat": "webp",
"imageOutputQuality": 80,
"inferenceStepCount": 28,
"additionalLoraScale": 1,
"imagePromptStrength": 0.8,
"diffusionGuidanceScale": 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 empowers developers to create stunning visuals effortlessly. With its flexible input options and powerful generation capabilities, it opens the door to a wide array of applications, from artistic endeavors to practical business solutions. As you explore this API, consider how it can enhance your projects and streamline your image creation process. Start integrating today and unlock your creative potential!