Create Stunning Images with the Image Generation API

The Image Generation And Prediction API offers developers a powerful tool to create and manipulate images using advanced inpainting techniques. This API simplifies the process of generating high-quality images tailored to specific prompts, enabling rapid prototyping and creative expression. With the flexibility to customize dimensions, quality settings, and styles, developers can unlock endless possibilities for their applications.
Common use cases include generating unique artwork, enhancing product visuals, creating custom graphics for marketing materials, and even developing engaging content for social media. By leveraging this API, developers can save time and resources while producing visually appealing results that resonate with their target audiences.
To get started, you'll need an API key for the Cognitive Actions service and a basic understanding of making API calls.
Generate Image with Inpainting
The Generate Image with Inpainting action allows developers to create images using sophisticated image inpainting techniques. This action is designed to solve the challenge of generating new images or modifying existing ones based on specified prompts, making it ideal for creative projects that require customization.
Input Requirements
To use this action, you'll need to provide the following inputs:
- prompt: A detailed description that guides the image generation.
- image (optional): An input image for inpainting mode.
- mask (optional): An image mask for inpainting.
- model: Choose between 'dev' for detailed predictions or 'schnell' for faster results.
- width and height: Specify custom dimensions if the aspect ratio is set to custom.
- goFast: A boolean to enable faster predictions.
- guidanceScale: Adjust the diffusion process for realism.
- numberOfOutputs: Define how many images to generate.
Expected Output
The output will be a generated image URL, which can be accessed directly for use in applications. The image can be in various formats such as webp, jpg, or png, based on the specified output format.
Use Cases for this Specific Action
This action is particularly useful when:
- You want to generate custom illustrations for digital content.
- You're developing a game and need unique character designs or backgrounds.
- You need to create marketing visuals that stand out from stock images.
- The project requires rapid iteration on visual concepts based on user feedback.
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 = "c59c9c6d-f904-4257-8237-473f349f1f6a" # Action ID for: Generate Image with Inpainting
# 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/MzrmVJXJpzyZg2LlBSzbHnUqBMtHEZcQfRUVC9G8jFd5UZj3/lady_060_winehouseOG.png",
"model": "dev",
"goFast": false,
"prompt": "bespoke, 24x24 pixel grid portrait, symbolic punk style, purple solid background, black hair with red accessory positioned at x=15-17 y=7-9, black pupils fixed at (8,12) and (13,12), dark iris eyes at (9,12) and (14,12), black nose dot at (11,13), natural pink lips spanning x=10-13 y=15-16, light skin tone with jaw defined at y=19, subtle natural makeup aesthetic, subtle eyebrows at y=11, right-facing, pure pixel art with no gradients or anti-aliasing, clean color boundaries, all features contained within 22x22 center grid",
"guidanceScale": 3,
"mainLoraScale": 1,
"outputQuality": 80,
"numberOfOutputs": 1,
"promptIntensity": 0.8,
"imageAspectRatio": "1:1",
"imageOutputFormat": "webp",
"additionalLoraScale": 1,
"approximateMegapixels": "1",
"numberOfInferenceSteps": 28
}
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 provides developers with a versatile solution for creating and customizing images. By utilizing the Generate Image with Inpainting action, you can efficiently produce high-quality visuals tailored to your specific needs. Whether for art, marketing, or game design, this API opens up a world of creative possibilities.
To maximize your use of this API, consider experimenting with different prompts, models, and settings to achieve the best results for your projects. Start integrating this powerful tool into your applications today and elevate your visual content to new heights.