Transform Your Images with Inpainting Cognitive Actions

In the world of digital media, the ability to manipulate and enhance images can dramatically change the way we present content. The Inpainting Cognitive Actions service allows developers to leverage advanced image processing techniques to modify images effectively. With the ability to use masks and prompts, this service simplifies the process of altering specific areas in an image, opening up a realm of creative possibilities.
Imagine needing to remove unwanted objects from a photo, fill in missing parts of an image, or apply artistic changes without affecting the entire picture. Inpainting delivers these capabilities swiftly and efficiently, making it an invaluable tool for developers working in fields such as graphic design, marketing, and content creation.
Prerequisites
To get started with Inpainting Cognitive Actions, you will need an API key for the Cognitive Actions service and a basic understanding of how to make API calls.
Perform Image Inpainting
The Perform Image Inpainting action allows users to define specific areas in an image that they want to modify using a mask. By providing a textual prompt, you can guide the modifications, enabling a high level of customization in the output images.
Purpose
This action is particularly useful for scenarios where you need to change or enhance parts of an image without performing a complete overhaul. Whether you want to repair an old photo, create unique artwork, or generate variations of an original image, this action can help you achieve that.
Input Requirements
To use this action, you need to provide the following inputs:
- image: The original image from which variations will be generated, provided as a URI.
- mask: A black and white image that indicates where changes should occur, also provided as a URI.
- prompt: A textual description that guides the generation process (e.g., "Highly detailed realistic Jeep car model, rendered in 3dsmax").
- scheduler: An optional parameter to select the scheduling method for the image generation process (default is "DPMSolverMultistep").
- guidanceScale: A numeric value that influences how closely the output adheres to the input prompt.
- negativePrompt: Optional items or concepts to exclude from the output.
- numberOfOutputs: Specifies how many images to generate (default is 1).
- numberOfInferenceSteps: The number of denoising steps during image generation, affecting the output's detail.
Example Input
{
"mask": "https://replicate.delivery/pbxt/JoabVWoz0LdM8bDMJv5isfK3H7VVEkrdQUkBKSR5ogkHQPWR/qr2ai-mask.jpg",
"seed": 3245345435,
"image": "https://replicate.delivery/pbxt/JoabVk9z5DgmfubFa8lq4ubzuh3w01YF6BuNx7uTFMJIONJe/0_0%20%281%29.webp",
"prompt": "Highly detailed realistic Jeep car model, rendered in 3dsmax, smooth soft lighting, polycount, concept art",
"scheduler": "DPMSolverMultistep",
"guidanceScale": 7.5,
"numberOfOutputs": 1,
"numberOfInferenceSteps": 50
}
Expected Output
The output will be a generated image that reflects the modifications defined by the mask and prompt. The result will be a URI linking to the modified image.
Example Output
[
"https://assets.cognitiveactions.com/invocations/74f49a47-d5c9-4658-bd21-6702129f3534/df241bed-0595-41de-9209-23a0dc871c66.png"
]
Use Cases for this Action
- Object Removal: Remove unwanted elements from images, such as people or objects that detract from a scene.
- Creative Art Generation: Generate artistic interpretations of images by applying different styles or themes.
- Restoration: Fill in missing parts of damaged photographs, enhancing visual appeal and preserving memories.
- Marketing Materials: Create tailored visuals for advertising campaigns by modifying images to better fit branding guidelines.
- Content Creation: Enable content creators to generate unique visuals for blogs, social media, and other platforms.
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 = "4d59eca8-da6e-4894-99b3-aca0e53330d5" # 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 = {
"mask": "https://replicate.delivery/pbxt/JoabVWoz0LdM8bDMJv5isfK3H7VVEkrdQUkBKSR5ogkHQPWR/qr2ai-mask.jpg",
"seed": 3245345435,
"image": "https://replicate.delivery/pbxt/JoabVk9z5DgmfubFa8lq4ubzuh3w01YF6BuNx7uTFMJIONJe/0_0%20%281%29.webp",
"prompt": "Highly detailed realistic Jeep car model, rendered in 3dsmax, smooth soft lighting, polycount, concept art",
"scheduler": "DPMSolverMultistep",
"guidanceScale": 7.5,
"numberOfOutputs": 1,
"numberOfInferenceSteps": 50
}
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 Inpainting Cognitive Action provides developers with a powerful tool for image manipulation, allowing for creative freedom and efficiency. By utilizing masks and prompts, you can easily modify images to meet various needs, from artistic expression to practical applications in marketing and content creation.
Start exploring the possibilities of Inpainting today, and elevate your image processing capabilities to new heights!