Enhance Your Images with Stable Diffusion V2 Inpainting

25 Apr 2025
Enhance Your Images with Stable Diffusion V2 Inpainting

In the world of image editing, the ability to modify specific areas of an image while preserving the rest is invaluable. The Stable Diffusion V2 Inpainting service offers developers a powerful solution for advanced image manipulation. By leveraging cutting-edge inpainting techniques, this service allows you to fill in or alter parts of an image based on user-defined masks and descriptive text prompts. Whether for artistic purposes, content creation, or enhancing user-generated content, this API simplifies complex image editing tasks, enabling faster and more creative workflows.

Common use cases for this service include:

  • Artistic Creation: Artists can use inpainting to modify images with new elements, enhancing their creative projects.
  • Content Restoration: Restore damaged or incomplete images by smartly filling in missing parts based on context.
  • Product Design: Modify images of products to visualize different designs or colors without needing to create new images from scratch.
  • Game Development: Create variations of game assets by altering specific parts of images while keeping the overall design intact.

To get started, ensure you have a Cognitive Actions API key and a basic understanding of making API calls.

Perform Inpainting with Stable Diffusion V2

This action utilizes the Stable Diffusion V2 model to execute inpainting on images. It allows you to modify specific regions of an image, guided by provided mask inputs and text prompts. This capability is particularly useful for tasks that require precision in image editing, as it combines mask-generation strategies with latent VAE representations to produce high-quality results.

Input Requirements

To use this action, you need to provide the following inputs:

  • image (string): The URI of the initial image you wish to modify. The image should ideally be 512x512 pixels.
  • mask (string): A black and white mask image URI where black areas will be inpainted, and white areas will remain unchanged.
  • prompt (string): A descriptive text prompt that guides the inpainting process. Defaults to "a photo of an astronaut riding a horse on mars."
  • guidanceScale (number): A value between 1 and 20 that determines the influence of the prompt; the default is 7.5.
  • promptStrength (number): A value that dictates how much the prompt influences the image; 1.0 will completely override the initial image.
  • numberOfOutputs (integer): The number of images to generate; can range from 1 to 8.
  • numberOfInferenceSteps (integer): The number of denoising steps during image generation, ranging from 1 to 500.

Expected Output

The expected output is a list of image URIs that correspond to the modified images based on the input parameters. For example, one output might look like this:

  • https://assets.cognitiveactions.com/invocations/e413c350-6c56-4dbb-b158-f9baf547295a/a82b87d2-e468-4e79-aa63-1dc4922de6af.png

Use Cases for this Specific Action

  • Creative Projects: Artists can generate unique variations of their work by inpainting specific elements, allowing for endless possibilities in design.
  • Image Restoration: Perfect for photographers and archivists looking to restore historical images or remove unwanted elements while maintaining the original aesthetic.
  • Marketing Materials: Marketers can quickly create tailored visuals for campaigns by modifying existing images to fit new themes or messages.
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 = "e59cf3b7-d550-49a0-80e3-5e01eb1cf68b" # Action ID for: Perform Inpainting with Stable Diffusion V2

# 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/HszfSoPGhMEvXf9Gf5gA564aMMyuki3TLSlQQmCepokDJy47/mask.png",
  "image": "https://replicate.delivery/pbxt/HszfS11BnkFTWHKv6sa5kQgrGGcLk0r1ihgUVAZp2bQJ0Yq9/dog.png",
  "prompt": "face of a ginger cat, high resolution, sitting on a park bench",
  "guidanceScale": 7.5,
  "promptStrength": 0.8,
  "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 Stable Diffusion V2 Inpainting service offers developers an innovative approach to image editing, providing the ability to alter images with precision while preserving other elements. Its versatility makes it ideal for various applications, from artistic endeavors to practical restoration tasks. By integrating this powerful API, you can enhance your projects, streamline workflows, and unleash your creativity. Consider exploring the diverse possibilities this service offers and see how it can elevate your image processing capabilities.