Create Stunning Images with the Random Shapes Cognitive Actions

In the realm of image generation, the andreasjansson/random-shapes Cognitive Actions offer an exciting way to create visually appealing graphics with minimal effort. The primary action, Generate Random Shapes, allows developers to produce images filled with randomly generated rectangles, ellipses, and lines in various colors. By utilizing this pre-built action, you can easily enhance your applications with unique and engaging visuals.
Prerequisites
Before you dive into integrating Cognitive Actions into your application, ensure you have the following:
- API Key: You will need an API key to authenticate your requests to the Cognitive Actions platform. This key is typically passed in the headers of your requests to verify your identity and access the service.
- Basic Understanding of JSON: Familiarity with JSON structure is beneficial as you will be constructing JSON payloads for your requests.
Cognitive Actions Overview
Generate Random Shapes
The Generate Random Shapes action is designed to create an image composed of randomly generated geometric shapes. This functionality allows for customization such as width, height, and a random seed to ensure deterministic outputs.
Input
The input for this action is defined by the following schema:
{
"seed": 12345,
"width": 1024,
"height": 1024
}
- seed (optional): An integer used as the random seed for generating deterministic outputs. Adjust this value to generate different results.
- width (required): The width of the output image in pixels. Defaults to 256, with a valid range of 128 to 1440.
- height (required): The height of the output image in pixels. Defaults to 256, with a valid range of 128 to 1440.
Example Input:
{
"width": 1024,
"height": 1024
}
Output
The output of the action is a link to the generated image. An example output might look like this:
https://assets.cognitiveactions.com/invocations/2b1337ca-0f24-4f61-ad38-448140a2e78f/b6c74b99-f4e4-4aca-bc9c-65cd0d42f171.png
This URL directs you to the generated image, which contains the random shapes based on your specified parameters.
Conceptual Usage Example (Python)
Here's a conceptual example of how to execute the Generate Random Shapes 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 = "f9a45d66-eafc-44a9-a554-b8cfbaa3fbea" # Action ID for Generate Random Shapes
# Construct the input payload based on the action's requirements
payload = {
"width": 1024,
"height": 1024
}
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 snippet, replace YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key. The action ID corresponds to the Generate Random Shapes action, and the payload structure adheres to the input schema. The endpoint URL and request structure are illustrative; ensure to check the specific API documentation for precise details.
Conclusion
Utilizing the Generate Random Shapes Cognitive Action opens up a world of possibilities for creating diverse and captivating images in your applications. By customizing parameters like width, height, and seed, you can generate a wide variety of outputs tailored to your needs. As you explore these capabilities, consider how you might integrate them into your projects for enhanced visual impact. Happy coding!