Transform Your Images with Style Transfer Using CCPL Actions

24 Apr 2025
Transform Your Images with Style Transfer Using CCPL Actions

In the world of image processing, style transfer has emerged as a powerful technique, allowing developers to create stunning visual content by blending the essence of one image with another. The CCPL (Contrastive Coherence Preserving Loss) Cognitive Actions offer a versatile solution for style transfer, catering to both photo-realistic and artistic styles while maintaining image coherence and quality. This blog post will guide you through the capabilities of the CCPL actions and how to integrate them into your applications.

Prerequisites

Before diving into the integration of CCPL actions, ensure you have the following:

  • An API key for the Cognitive Actions platform, which will be used for authentication.
  • Basic understanding of JSON and how to structure API requests.

To authenticate your requests, you will typically pass your API key in the headers of your HTTP requests.

Cognitive Actions Overview

Perform Style Transfer with CCPL

The Perform Style Transfer with CCPL action uses the CCPL method to enable you to apply various styles to images. Whether you want to achieve a photo-realistic finish or an artistic flair, this action provides the flexibility to use any style image while ensuring high quality and coherence in the output.

Input

The input for this action requires the following fields:

  • content (string, required): The URI of the content image to be styled.
  • style (string, required): The URI of the style image to be used for style transfer.
  • mode (string, optional): Defines the style transfer mode, either 'photo-realistic' or 'art'. Defaults to 'photo-realistic'.
  • styleSize (integer, optional): Specifies the new minimum size for the style image. Retains the original size if set to 0. Defaults to 512.
  • contentSize (integer, optional): Specifies the new minimum size for the content image. Retains the original size if set to 0. Defaults to 512.

Example Input:

{
  "mode": "art",
  "style": "https://replicate.delivery/mgxm/73a38fc6-0713-4bb9-a2a4-e464bdd35c64/15.jpg",
  "content": "https://replicate.delivery/mgxm/96745438-6ed1-4231-b3c3-46b85937b062/1.jpg",
  "styleSize": 0,
  "contentSize": 0
}

Output

Upon successful execution, the action returns a URI pointing to the generated image. The output will typically look like this:

Example Output:

https://assets.cognitiveactions.com/invocations/eb22f8d3-6304-405d-83fb-34a1257eae3b/6ee7a3c3-2fd8-4f72-aef2-809e1ea1786b.png

Conceptual Usage Example (Python)

Here’s a conceptual Python code snippet illustrating how to call the CCPL action to perform style transfer:

import requests
import json

# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute" # Hypothetical endpoint

action_id = "e6d1868b-2c96-439e-9782-318eea684426" # Action ID for Perform Style Transfer with CCPL

# Construct the input payload based on the action's requirements
payload = {
    "mode": "art",
    "style": "https://replicate.delivery/mgxm/73a38fc6-0713-4bb9-a2a4-e464bdd35c64/15.jpg",
    "content": "https://replicate.delivery/mgxm/96745438-6ed1-4231-b3c3-46b85937b062/1.jpg",
    "styleSize": 0,
    "contentSize": 0
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json"
}

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json={"action_id": action_id, "inputs": payload} # Hypothetical structure
    )
    response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully:")
    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: {e.response.text}")

In this snippet, replace the COGNITIVE_ACTIONS_API_KEY with your actual API key. The action_id corresponds to the "Perform Style Transfer with CCPL" action. The payload structure must match the required input schema to ensure proper execution.

Conclusion

The CCPL Cognitive Actions provide a powerful tool for developers looking to leverage style transfer techniques in their applications. With the ability to create both photo-realistic and artistic images, the integration of these actions can significantly enhance your project's visual capabilities. Whether you are building an art application, a photo editing tool, or simply exploring creative possibilities, the CCPL actions are a valuable addition to your toolkit. Start experimenting with style transfer today and unlock new dimensions in image processing!