Create Stunning Custom Images with Inpainting API

12 May 2025
Create Stunning Custom Images with Inpainting API

The Image Generation and Inpainting API offers developers powerful capabilities to generate and modify images using advanced techniques. With this API, you can create custom images tailored to your specifications, whether through image-to-image transformations or inpainting methods. The ability to adjust dimensions, aspect ratios, and quality settings allows for personalized outputs that meet the unique needs of various projects.

This API is especially beneficial for developers looking to enhance applications in fields such as gaming, e-commerce, social media, and digital art, where eye-catching visuals are crucial. Imagine generating unique artwork, creating product images, or even modifying existing visuals to fit your brand's aesthetic—all with just a few API calls.

Prerequisites

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

Generate Custom Images with Inpainting

This operation allows users to generate custom images using image-to-image or inpainting techniques. It supports various adjustments such as dimensions, aspect ratios, and quality settings. Users can also enable faster generation and apply custom model weights or LoRA adjustments for tailored outputs.

Input Requirements

The input for this action is a JSON object that includes the following key properties:

  • prompt: A detailed description of the image you want to generate.
  • image: An optional input image for inpainting or image-to-image transformations.
  • mask: An optional mask for inpainting mode.
  • width and height: Custom dimensions for the output image (if aspect ratio is set to custom).
  • Other parameters like guidanceScale, outputQuality, and numberOfOutputs help refine the image generation process.

Expected Output

The output will be a URL pointing to the generated image in your specified format (e.g., webp, jpg). The output image will reflect the prompt provided, incorporating any adjustments or enhancements specified.

Use Cases for this Specific Action

  • Custom Art Generation: Create unique artwork for games, apps, or websites based on specific themes or styles.
  • Product Visualization: Generate tailored images for e-commerce platforms, allowing for distinct representation of products.
  • Content Creation: Enhance social media posts or blogs with custom visuals that align with content narratives.
  • Visual Prototyping: Quickly create visual assets for design prototypes, enabling rapid iteration and feedback.
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 = "95896be5-e1e1-4bf8-b382-c95e43523bdc" # Action ID for: Generate Custom Images with Inpainting

# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
  "image": "https://replicate.delivery/pbxt/MzbWqvohHLswKGnSf4zSFP4uv1NewJttr3OKJstWf2bEY9JH/lady_000_lemonOG.jpeg",
  "goFast": false,
  "prompt": "bespoke, 24x24 pixel grid portrait, symbolic punk style, vibrant yellow background, dark brown hair with red/white bandana at y=3–6, pupils at (8,12) and (13,12), blue irises at (9,12) and (14,12), black nose dot at (11,13), orange-red lips at x=10–13 y=15–16 with upward smile curve, light skin tone, subtle brows at y=11, clean pixel zones, right-facing, all elements within 22x22 grid",
  "loraScale": 1,
  "guidanceScale": 3,
  "outputQuality": 80,
  "extraLoraScale": 1,
  "inferenceModel": "dev",
  "inferenceSteps": 28,
  "promptStrength": 0.8,
  "imageMegapixels": "1",
  "numberOfOutputs": 1,
  "imageAspectRatio": "1:1",
  "imageOutputFormat": "webp"
}

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 Image Generation and Inpainting API empowers developers to create and modify images effortlessly, opening up a realm of possibilities for visual content. With its customizable features and fast processing capabilities, you can enhance your applications with stunning visuals tailored to your needs.

As a next step, consider integrating the API into your projects to explore the full potential of image generation and inpainting. Whether you're building an art platform or enhancing your e-commerce site, this API provides the tools you need to succeed.