Transform Your Images with AI-Powered Inpainting Actions

25 Apr 2025
Transform Your Images with AI-Powered Inpainting Actions

In today's digital age, the ability to modify images with precision and creativity is becoming increasingly important for developers. The "Test" service offers powerful Cognitive Actions that leverage advanced techniques like Stable Diffusion for image inpainting. This allows you to selectively modify images by filling in masked areas with new content or removing existing content, providing a seamless way to enhance visual media.

With these Cognitive Actions, developers can automate complex image processing tasks, saving time and effort while ensuring high-quality results. Whether you’re looking to create stunning visual effects, perform content-aware editing, or simply enhance images for marketing materials, these actions are designed to meet your needs.

Common use cases include:

  • Enhancing product images by replacing backgrounds or removing unwanted objects.
  • Creating artistic modifications by adding new elements to existing photos.
  • Generating unique content for social media or marketing campaigns.

Before diving into the actions, ensure you have your Cognitive Actions API key handy and a basic understanding of how to make API calls.

Perform Image Inpainting

The "Perform Image Inpainting" action is designed to modify images by filling in specified areas with new content or removing existing elements. This is particularly useful for developers working on applications that require dynamic image editing capabilities.

Input Requirements

To utilize this action, you’ll need to provide the following inputs:

  • image: The URI of the image you want to modify.
  • maskImage: A monochrome mask URI that indicates which areas of the image to alter (white areas will be repainted, while black areas stay unchanged).
  • method: Choose between "replace" to fill masked areas with new content or "remove" to erase the content.
  • prompt: A descriptive text that guides the inpainting process (e.g., "a black leather couch").
  • numOutputs: The number of output images to generate (1 to 8).
  • guidanceScale: A value that determines how strongly the prompt influences the results (1 to 20).
  • numInferenceSteps: The number of steps in the denoising process, which can affect image quality.
  • previewInputImage: A boolean to indicate if the output should include an overlay of the mask on the original image.
  • seed: An optional random seed for consistency across generations.
  • negativePrompt: Optional elements to exclude from the output.

Expected Output

The expected output will include one or more modified image URIs based on your specifications. These images will reflect the changes dictated by your prompt and mask, providing a variety of creative options.

Use Cases for this specific action

  • E-commerce: Quickly replace product backgrounds to create a clean, professional look.
  • Content Creation: Effortlessly modify images for blogs and social media posts to match branding.
  • Artistic Projects: Enable creators to visualize concepts by adding or removing elements from their artwork.
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 = "381c7005-5ecb-4f18-a298-3bf9a5942280" # 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": 12345,
  "image": "https://replicate.delivery/pbxt/Ia2E3MUrVDVXITRVSKCHcCYi9WMYGoTpqxtqZ0PuqRoaY3FW/1.jpg",
  "method": "replace",
  "prompt": "a black leather couch",
  "maskImage": "https://replicate.delivery/pbxt/Ia2E2SDOE51oEfLeToezFAIYKw84oX0wHJYNVO5GUr6YMFMi/1_mask.jpg",
  "numOutputs": 3,
  "guidanceScale": 5,
  "numInferenceSteps": 20,
  "previewInputImage": true
}

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 Cognitive Actions provided by the "Test" service empower developers to transform images with ease and creativity. By leveraging advanced inpainting capabilities, you can enhance your applications, streamline workflows, and deliver stunning visual results. Whether for marketing, content creation, or artistic endeavors, these actions offer a robust solution for any image processing needs.

Take your image editing to the next level and explore the possibilities today!