Transform Your Imagery with the Image Generation and Inpainting API

The Image Generation and Inpainting API offers developers a powerful tool for creating and modifying images seamlessly. With the ability to generate high-quality visuals and refine existing images through inpainting, this API simplifies the creative process, allowing for quick and effective adjustments. Whether you're working on game design, digital art, or marketing materials, integrating this API can enhance your workflow and output quality.
Common use cases include generating unique character designs, creating variations of artwork, or filling in missing parts of an image while maintaining a cohesive look. By leveraging advanced models, developers can achieve tailored results that align with specific project requirements, ultimately saving time and boosting creativity.
Prerequisites
To get started with the Image Generation and Inpainting API, you will need an API key for authentication and a basic understanding of making API calls.
Perform Image Inpainting
The "Perform Image Inpainting" action allows you to modify specific areas of an image by using masks, enabling targeted enhancements while preserving the rest of the image. This functionality is incredibly useful for correcting visuals, adding elements, or removing unwanted features without starting from scratch.
Input Requirements
To utilize this action, you must provide the following inputs:
- Mask: A URI that indicates which areas of the image to modify (white areas will be altered, while black areas remain unchanged).
- Image: The base image for processing, also provided as a URI.
- Seed: An integer to control randomness; set to -1 for a random seed.
- Steps: An integer ranging from 1 to 100, determining the complexity of the generation process.
- Width and Height: Dimensions of the output image in pixels, both capped at 4096.
- Prompt: A string that guides the generation, with a default value of "1girl".
- Strength: A number that adjusts the noise level during transformations, with a range from 0 to 1.
- Batch Size: The number of images to generate in one request, from 1 to 4.
- Model Name: Specify which model to use for generation, defaulting to "miaomiaoHarem_vpred11".
- Additional parameters include options for configuration scale, exclusion prompts, and task schedulers, among others.
Expected Output
The expected output is a URI pointing to the generated image, which reflects the modifications specified in the input mask.
Use Cases for this specific action
This action is ideal for:
- Digital Artists looking to refine their artwork by seamlessly integrating new elements.
- Game Developers who need to create or alter character designs while maintaining style consistency.
- Marketing Teams that require quick modifications to promotional images, ensuring visual appeal without extensive design overhauls.
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 = "e6dc685d-02f6-4cc4-b0ff-e85d68f08631" # Action ID for: Perform Image Inpainting
# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
"seed": -1,
"steps": 35,
"width": 1184,
"height": 864,
"prompt": "1girl",
"strength": 0.7,
"batchSize": 1,
"modelName": "miaomiaoHarem_vpred11",
"configScale": 5,
"addPreprompt": true,
"clipLayerSkip": 1,
"taskScheduler": "Euler a",
"exclusionPrompt": "animal, cat, dog, big breasts",
"paginationScale": 3,
"inpaintingBlurFactor": 5,
"noiseGuidanceRescale": 0.5,
"variationalAutoencoder": "default"
}
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 elevate their visual projects by providing robust tools for image creation and modification. With its flexibility and ease of integration, this API can significantly streamline workflows across various domains, from gaming to marketing. To harness the full potential of this API, start exploring its capabilities today, and unlock a new realm of creative possibilities!