Create Stunning Bokeh Effects Effortlessly with Bokeh Prediction

26 Apr 2025
Create Stunning Bokeh Effects Effortlessly with Bokeh Prediction

In the world of photography, the bokeh effect is a beloved technique that adds artistic flair and depth to images by blurring the background while keeping the subject in sharp focus. The Bokeh Prediction service offers developers an innovative Cognitive Action that leverages the BokehMe framework to generate high-resolution bokeh effects on images. This tool not only simplifies the process of enhancing visual aesthetics but also provides flexibility in adjusting blur size, focal plane, and aperture shape, making it a powerful ally for photographers, designers, and content creators.

Imagine being able to transform ordinary images into captivating visuals with just a few parameters. Whether you're designing marketing materials, enhancing web content, or creating stunning social media posts, the ability to apply photorealistic bokeh effects can significantly elevate the quality of your work. Bokeh Prediction enables you to focus on the creative aspects of your project while streamlining the technical details, allowing for faster and more efficient workflows.

Prerequisites

To get started with Bokeh Prediction's Cognitive Actions, you will need an API key for the Cognitive Actions service and a basic understanding of making API calls.

Generate High-Resolution Bokeh Effects

The "Generate High-Resolution Bokeh Effects" action is designed to apply stunning bokeh effects to images, enhancing their visual appeal. By integrating both neural and classical rendering techniques, this action produces photorealistic results that can be adjusted to fit specific artistic needs.

Input Requirements

To utilize this action, you need to prepare a JSON object with the following parameters:

  • image: The URI of the image to be processed. This is a required field.
  • focus: A number that specifies the focus distance for the bokeh effect. The default value is 0.8.
  • xCoordFocus: An integer indicating the X-coordinate on the image where focus should be applied. Default is 0, meaning it uses the focus distance.
  • yCoordFocus: An integer indicating the Y-coordinate on the image for focus. Default is also 0.
  • blurStrength: A number that defines the intensity of the blur effect. The default value is 10.

Example Input:

{
  "focus": 0.87,
  "image": "https://replicate.delivery/pbxt/KIwMwjiIssQuFO7dAFO9X8hZOZSNmbpUlrGh0WRFH4oTuvhK/If-Elon-Musk-Had-Been-a-Happy-Child%2C-Would-He-Still-Be-Launching-Rockets--Business-Redux-h_16082330.webp",
  "blurStrength": 20
}

Expected Output

The action will return a processed image with the bokeh effect applied, resulting in a visually appealing output that highlights the subject while softening the background.

Example Output:

https://assets.cognitiveactions.com/invocations/4c506d0f-dd55-487e-8e33-ea9238d8267e/9be5b491-8b88-4e20-aa10-7c381b95486d.png

Use Cases for this Action

  • Photography Enhancement: Photographers can use this action to add a professional touch to their images, drawing attention to the subject while creating a dreamy background.
  • Marketing Materials: Marketers can create engaging visuals for advertisements or social media campaigns that stand out in crowded feeds.
  • Web Design: Designers can enhance website images, making them more visually appealing and improving user engagement on various 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 = "a7642a67-2b8a-4fdb-b1fe-be54b0b809f6" # Action ID for: Generate High-Resolution Bokeh Effects

# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
  "focus": 0.87,
  "image": "https://replicate.delivery/pbxt/KIwMwjiIssQuFO7dAFO9X8hZOZSNmbpUlrGh0WRFH4oTuvhK/If-Elon-Musk-Had-Been-a-Happy-Child%2C-Would-He-Still-Be-Launching-Rockets--Business-Redux-h_16082330.webp",
  "blurStrength": 20
}

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

With Bokeh Prediction's ability to generate high-resolution bokeh effects, developers and creatives alike can effortlessly enhance their images, making them more captivating and professional. The flexibility in adjusting focus, blur strength, and focal coordinates provides endless possibilities for artistic expression. As you explore these Cognitive Actions, consider how they can be integrated into your projects to create visually stunning content that resonates with your audience. Get started today and elevate your image processing capabilities!