Enhance Your Creative Projects with Image Generation and Inpainting

26 Apr 2025
Enhance Your Creative Projects with Image Generation and Inpainting

In the world of digital content creation, the ability to generate stunning images quickly and effectively is invaluable. The "Andres2" service offers a powerful Cognitive Action called "Generate Image with Inpainting," designed to simplify the image creation process while providing developers with robust customization options. This action enables you to create unique images through inpainting techniques, allowing for both image-to-image transformations and entirely new image generation. By leveraging customizable settings such as seed values, image dimensions, guidance scales, and output quality, developers can achieve precise and high-quality results tailored to their specific needs.

Common use cases for this action include generating marketing visuals, enhancing existing images with new elements, and creating artwork for social media campaigns. Whether you are an artist looking to bring your vision to life or a marketer needing bespoke graphics, this image generation tool can dramatically speed up your workflow and enhance the quality of your outputs.

Before diving in, ensure you have a Cognitive Actions API key and a basic understanding of how to make API calls.

Generate Image with Inpainting

The "Generate Image with Inpainting" action allows developers to create new images or modify existing ones using advanced inpainting techniques. This action is particularly useful for tasks that require generating images based on detailed prompts or adjusting specific areas of an image to achieve the desired aesthetic.

Input Requirements

To utilize this action, you'll need to provide a structured input that includes:

  • Prompt: A descriptive text that guides the image generation process.
  • Image: An optional input image for inpainting or transformation.
  • Mask: If inpainting is required, this image mask specifies which parts of the original image to alter.
  • Model: Choose between different models optimized for precision or speed.
  • Aspect Ratio, Width, Height: Custom dimensions for the generated image, if applicable.
  • Guidance Scale: Adjusts how closely the output adheres to the prompt.
  • Output Quality: Sets the quality level for the generated image.

Here's an example of the input structure:

{
  "seed": 49542,
  "image": "https://replicate.delivery/pbxt/McfJoGU5yoru85Re9i3Txuw9ibPdTNRG0GSYBiERFcSE9Q1n/istockphoto-637234116-612x612.jpg",
  "model": "dev",
  "prompt": "A professional-looking 1:1 photo of andres...",
  "aspectRatio": "1:1",
  "outputFormat": "jpg",
  "guidanceScale": 3,
  "outputQuality": 80,
  "enableFastMode": false,
  "promptStrength": 0.8,
  "numberOfOutputs": 1,
  "primaryLoraScale": 1,
  "additionalLoraScale": 1,
  "inferenceStepsNumber": 28,
  "approximateMegapixels": "1"
}

Expected Output

The expected output is a URL linking to the generated image, which meets the specifications set in the input. For example:

[
  "https://assets.cognitiveactions.com/invocations/31da1e3d-c6da-4af6-8795-326a75773338/0b473f0c-5683-41c4-830c-10fcd825a45a.jpg"
]

Use Cases for this Specific Action

  • Creative Art Projects: Artists can use this action to generate original artworks based on detailed descriptions, allowing for greater creativity and expression.
  • Marketing and Branding: Marketers can create tailored visuals for campaigns, ensuring brand consistency while saving time on design work.
  • Content Enhancement: Use inpainting to modify existing images, adding or removing elements to better fit the desired narrative or aesthetic.
  • Prototyping Designs: Developers can quickly generate mockups for applications or websites, facilitating faster feedback and iteration cycles.
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 = "8b6c37c2-f5bf-44ae-ae2e-8c7b46f0d806" # 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 = {
  "seed": 49542,
  "image": "https://replicate.delivery/pbxt/McfJoGU5yoru85Re9i3Txuw9ibPdTNRG0GSYBiERFcSE9Q1n/istockphoto-637234116-612x612.jpg",
  "model": "dev",
  "prompt": "A professional-looking 1:1 photo of andres , he is sitting at a desk in a modern office at night. He is wearing a dark blue blazer over a black t shirt, showing himself confidently at the camera. His hands are resting on the desk, with one hand over the other. A silver laptop is open on the desk, along with a clipboard with some documents and a pen. The background features large glass windows reflecting city lights, creating a sleek and contemporary office atmosphere. The lighting is soft and professional, giving a warm and approachable feel to the image.",
  "aspectRatio": "1:1",
  "outputFormat": "jpg",
  "guidanceScale": 3,
  "outputQuality": 80,
  "enableFastMode": false,
  "promptStrength": 0.8,
  "numberOfOutputs": 1,
  "primaryLoraScale": 1,
  "additionalLoraScale": 1,
  "inferenceStepsNumber": 28,
  "approximateMegapixels": "1"
}

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 Andres2 service empowers developers to produce high-quality images efficiently. With its versatile capabilities for both new image generation and modification, this action is ideal for a wide range of applications—from artistic endeavors to commercial projects. By incorporating this tool into your workflow, you can enhance creativity, streamline processes, and achieve stunning visual results.

Ready to start generating images? Explore the possibilities with the Cognitive Actions API and transform your creative projects today!