Create Stunning Images Effortlessly with AI Inpainting Actions

In the age of visual content, the ability to generate and refine images on demand is a game changer for developers and creators alike. The "App" service offers a powerful Cognitive Action to generate images using text prompts, enabling users to craft unique visuals with options for inpainting and refinement. This feature simplifies the image creation process, allowing for customization through parameters such as image size, prompt strength, and guidance scale.
Why Use This Action?
Imagine needing an image that perfectly fits your project but can't find one that meets your requirements. With the Generate Image with Inpainting and Refinement action, you can create images tailored to your specific needs in mere moments. Whether you're developing a marketing campaign, designing a game, or enhancing a website, this action can help you produce stunning visuals quickly and efficiently.
Prerequisites
To get started with the Cognitive Actions API, you'll need an API key and a basic understanding of making API calls.
Generate Image with Inpainting and Refinement
The Generate Image with Inpainting and Refinement action allows you to create images from text prompts while offering options for customization and refinement. This action is particularly useful for scenarios where you want to enhance or modify existing images, making it a versatile tool in your development toolkit.
Input Requirements:
- Mask: URI for the input mask used in inpaint mode. Black areas will be preserved, while white areas will be inpainted.
- Seed: An integer for random number generation (optional).
- Image: URI of the input image for img2img or inpaint mode.
- Width and Height: Dimensions of the output image in pixels (default is 1024x1024).
- Prompt: A descriptive text prompt for the image generation (e.g., "An astronaut riding a rainbow unicorn").
- LoRA Scale: A scaling factor for trained models, ranging from 0 to 1 (default 0.6).
- Refinement Style: Choose from no_refiner, expert_ensemble_refiner, or base_image_refiner (default is no_refiner).
- Guidance Scale: A scale factor for classifier-free guidance, ranging from 1 to 50 (default 7.5).
- Apply Watermark: Boolean to indicate if a watermark should be applied (default is true).
- Prompt Strength: Intensity of the prompt in img2img or inpaint modes (default 0.8).
- Output Image Count: Number of output images to generate, ranging from 1 to 4 (default is 1).
- High Noise Fraction: Proportion of noise applied when using expert_ensemble_refiner (default 0.8).
- Inference Step Count: Number of denoising inference steps, ranging from 1 to 500 (default 50).
- Scheduling Algorithm: Algorithm used to schedule the denoising process (default is K_EULER).
- Unwanted Prompt Elements: Elements to avoid in the generated image (optional).
Expected Output: Upon successful execution, the action will return a list of URIs pointing to the generated images, which can be directly used in your applications.
Use Cases for this Action:
- Content Creation: Quickly generate images for blog posts, social media, or marketing materials.
- Game Development: Create unique assets tailored to game environments and characters.
- Design Prototyping: Visualize concepts and ideas before committing to full-scale designs.
- Personal Projects: Enhance personal projects with custom imagery that reflects your vision.
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 = "92d48e72-f9d7-4956-ad19-936583eb9a87" # Action ID for: Generate Image with Inpainting and Refinement
# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
"width": 1024,
"height": 1024,
"prompt": "a photo of TOK on the beach",
"loraScale": 0.6,
"refineStyle": "no_refiner",
"guidanceScale": 7.5,
"applyWatermark": true,
"promptStrength": 0.8,
"outputImageCount": 4,
"highNoiseFraction": 0.8,
"inferenceStepCount": 50,
"schedulingAlgorithm": "K_EULER",
"unwantedPromptElements": ""
}
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 Generate Image with Inpainting and Refinement action offers an efficient way to create and customize images tailored to your needs. With its extensive options and capabilities, developers can leverage this tool for a range of applications, from marketing to game development. Start integrating this action into your projects today and elevate your visual content creation!