Create Stunning Images with Inpainting Using AI Actions

In today's digital landscape, the ability to generate and manipulate images with precision and creativity is invaluable. The "Train" service offers a powerful Cognitive Action specifically designed for image generation through inpainting. This action allows developers to create stunning visuals by filling in or modifying parts of an image based on user-defined prompts and masks. With its support for multiple models, "Train" not only enhances the creative process but also optimizes performance and speed according to specific needs.
Imagine scenarios where you need to generate promotional graphics, enhance product images, or create unique artwork. The ability to guide the generation process with detailed prompts ensures that the resulting images align closely with your vision. This capability is particularly useful in industries like e-commerce, marketing, and graphic design, where visual content plays a crucial role in engagement and conversion.
Prerequisites
To get started with the "Train" service, ensure you have a valid Cognitive Actions API key and a basic understanding of making API calls.
Generate Image with Inpainting
The "Generate Image with Inpainting" action empowers developers to create images by filling in or altering parts of an existing image based on a specified mask and a creative prompt. This action is categorized under image generation, providing a versatile tool for various visual projects.
Purpose
This action addresses the need for seamless image creation and modification. By utilizing inpainting, you can efficiently produce images that meet specific requirements, whether for creative projects, product visualizations, or artistic endeavors.
Input Requirements
The input for this action requires a structured request including:
- imagePrompt (required): A text prompt that describes the desired features of the generated image.
- mask (optional): A URI of an image mask to specify areas for inpainting.
- image (optional): A URI of the input image for modifications.
- Additional parameters such as aspect ratio, output format, and model selection enhance customization.
Example Input
{
"loraScale": 1,
"aspectRatio": "9:16",
"imagePrompt": "Ultra-realistic image of a man wearing a dark olive-brown quilted jacket with a zipper closure and a cream fur-lined collar.",
"outputFormat": "webp",
"guidanceScale": 3.5,
"outputQuality": 90,
"extraLoraScale": 1,
"inferenceModel": "dev",
"promptStrength": 0.8,
"numberOfOutputs": 1,
"numInferenceSteps": 28
}
Expected Output
The output will be a generated image based on the input specifications, delivered in the requested format (e.g., webp, jpg, png). For instance, an output image might be returned as a URL link.
Example Output
https://assets.cognitiveactions.com/invocations/7c823e85-d707-436a-9bb7-0f6709ae14aa/74873b09-5239-4fb4-bc77-6e582ed6fb7b.webp
Use Cases for This Action
- E-commerce Product Images: Quickly generate high-quality product images with variations based on customer preferences.
- Marketing Material: Create promotional graphics that align with specific campaigns, ensuring that the visuals are engaging and relevant.
- Artistic Projects: Enable artists and designers to explore creative concepts by generating unique artwork based on guided prompts and existing images.
- Content Creation: Enhance blogs or social media posts with customized visuals that capture attention and convey messages effectively.
```python
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 = "da4470c8-0e37-4e9d-8040-d4781444167b" # 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 = {
"loraScale": 1,
"aspectRatio": "9:16",
"imagePrompt": "Ultra-realistic image of a men wearing a dark olive-brown quilted jacket with a zipper closure and a cream fur-lined collar. The jacket has horizontal stitching details throughout, two zippered side pockets, and slightly puffed sleeves with elastic cuffs. The men is standing in a professional studio with a completely white background, with soft lighting and no shadows or additional objects. The texture of the fabric should reflect the matte finish, ensuring that the jacket looks identical to the reference image. Focus on capturing the stitching, zipper, and fur lining accurately. TOK.",
"outputFormat": "webp",
"guidanceScale": 3.5,
"outputQuality": 90,
"extraLoraScale": 1,
"inferenceModel": "dev",
"promptStrength": 0.8,
"numberOfOutputs": 1,
"numInferenceSteps": 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 "Generate Image with Inpainting" action within the "Train" service offers developers a powerful tool for creating and modifying images with precision. By leveraging detailed prompts and optional masks, you can generate visuals that meet specific needs across various industries. As a next step, consider integrating this action into your projects to streamline image creation processes and enhance visual storytelling. Whether for marketing, e-commerce, or artistic endeavors, the possibilities are vast and exciting.