Create Stunning Custom Art with the Differentiable Rasterizer Cognitive Actions

24 Apr 2025
Create Stunning Custom Art with the Differentiable Rasterizer Cognitive Actions

In the world of generative art, the ltejedor/differentiable-rasterizer-vector-graphics API provides developers with powerful tools to create visually appealing images through customizable parameters. One of the primary actions available in this spec is the ability to generate controllable collage art, allowing for the creation of personalized artwork that can be tailored to specific preferences. This blog post will guide you through using this action effectively, showcasing its capabilities and providing a conceptual understanding of how to integrate it into your applications.

Prerequisites

Before diving into the Cognitive Actions, ensure you have the following:

  • An API key for the Cognitive Actions platform to authenticate your requests.
  • Basic knowledge of making HTTP requests and handling JSON data in your programming language of choice, like Python.

Authentication typically involves passing your API key in the request headers, ensuring secure access to the service.

Cognitive Actions Overview

Generate Controllable Collage Art

The Generate Controllable Collage Art action allows users to create customizable art collages using generative AI. You can adjust parameters such as color space, target image, and optimization steps to craft high-quality, personalized artwork.

Input

The input for this action is defined by the CompositeRequest schema, which includes several parameters:

  • loss (string): Specifies the loss function to optimize the collage. Options are CLIP and MSE. If MSE is chosen, the prompt will not affect the image, and targetImage becomes mandatory.
  • prompt (string): A global prompt for generating the collage. Default is a photorealistic depiction, e.g., "red panda".
  • patchURL (string): URL for an .npy file containing the collage patches. Default provided.
  • colorSpace (string): Defines the color space for image processing. Choices are RGB space or HSV space.
  • targetImage (string): A URI for the image file used with MSE loss; required when loss is set to MSE.
  • backgroundRed (integer), backgroundGreen (integer), backgroundBlue (integer): RGB values for background color.
  • numberOfPatches (integer): The initial number of images (patches) to include in the collage, defaulting to 100.
  • initialPositions (array): Starting positions of each patch in the collage.
  • optimizationSteps (integer): Number of steps for optimizing the collage creation, default is 250.
  • initialColors (array), initialTransformations (array): Optional arrays for setting the initial colors and transformations.

Example Input:

{
  "prompt": "red panda",
  "initialPositions": [
    ["image_5.png", -0.194643, 0.121429],
    ["image_4.png", 0.301786, -0.217857],
    ["image_8.png", 0.294643, 0.35],
    ["image_9.png", -0.648214, 0.157143]
  ]
}

Output

The output from this action is a list of arrays containing URLs to the generated images and associated metadata:

Example Output:

[
  [
    "https://assets.cognitiveactions.com/invocations/6bfd2999-db10-49ea-a5e3-72f8bab6cd8d/bb8fdb0e-620c-4f20-a472-39fa3014749b.png",
    "https://assets.cognitiveactions.com/invocations/6bfd2999-db10-49ea-a5e3-72f8bab6cd8d/c2709f8d-9594-472b-8625-bee0b099b6fe.json"
  ],
  ...
]

Conceptual Usage Example (Python)

Here’s a conceptual example of how you might call this action using Python:

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 = "89bd3049-2eac-46a6-a422-10012a78e85e"  # Action ID for Generate Controllable Collage Art

# Construct the input payload based on the action's requirements
payload = {
    "prompt": "red panda",
    "initialPositions": [
        ["image_5.png", -0.194643, 0.121429],
        ["image_4.png", 0.301786, -0.217857],
        ["image_8.png", 0.294643, 0.35],
        ["image_9.png", -0.648214, 0.157143]
    ]
}

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 code, you replace the COGNITIVE_ACTIONS_API_KEY and endpoint with your actual values. The payload is structured according to the required input for generating the collage. The response from the API will provide you with the URLs to your generated images.

Conclusion

The Generate Controllable Collage Art action opens up exciting possibilities for developers looking to create personalized artwork through generative AI. By leveraging customizable parameters, you can produce unique and high-quality collages tailored to your needs.

As a next step, consider experimenting with different prompts and configurations to see how the output changes. Whether for artistic projects or practical applications, this action can enhance your visual creations significantly!