Create Stunning Images with the justswim/em Cognitive Actions

In the realm of digital art and image manipulation, the justswim/em API provides a powerful set of Cognitive Actions that enable developers to generate and refine images through advanced techniques like inpainting and prompt-based guidance. These pre-built actions simplify the process of creating unique images tailored to specific requirements, making it easier for developers to integrate sophisticated image generation capabilities into their applications.
Prerequisites
Before you dive into using the Cognitive Actions, ensure you have the following:
- API Key: You'll need a valid API key for authentication. This key is usually passed in the headers of your API requests.
- Environment Setup: Make sure you have a suitable development environment with access to the requests library in Python.
Authentication typically involves including your API key in the request headers as follows:
headers = {
"Authorization": f"Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
Cognitive Actions Overview
Generate Image with Inpainting
The Generate Image with Inpainting action allows you to create images using inpainting techniques guided by user-defined prompts. It supports various parameters that give you control over the output, including image dimensions, noise levels, and whether to apply watermarks or safety checks.
Input
The input schema for this action includes several fields:
- mask (string, optional): URI of the input mask for inpainting. Black areas are preserved; white areas are inpainted.
- seed (integer, optional): Random seed for generating variations. If left blank, the seed is randomized.
- image (string, required): URI of the input image for 'img2img' or 'inpaint' mode.
- width (integer, default: 1024): Width of the output image in pixels.
- height (integer, default: 1024): Height of the output image in pixels.
- prompt (string, required): Main text prompt guiding the image generation.
- additiveScale (number, default: 0.6): Scale factor for LoRA model influence.
- inferenceSteps (integer, default: 50): Number of iterations for denoising.
- invertedPrompt (string, optional): Negative prompt to avoid certain elements.
- attachWatermark (boolean, default: true): Whether to apply a watermark to generated images.
- numberOfOutputs (integer, default: 1): Number of images to generate (1-4).
- promptIntensity (number, default: 0.8): Strength of the prompt effect on the generated image.
- refinementStyle (string, default: "no_refiner"): Style of refinement for the image.
- highNoiseFraction (number, default: 0.8): Noise fraction for the 'expert_ensemble_refiner'.
- schedulingAlgorithm (string, default: "K_EULER"): Algorithm for step scheduling.
- safetyCheckerDisabled (boolean, default: false): Option to disable the safety checker.
Example Input:
{
"width": 1024,
"height": 1024,
"prompt": "A sketch digital image of an astronaut in the style of TOK",
"additiveScale": 0.6,
"inferenceSteps": 50,
"invertedPrompt": "background",
"attachWatermark": true,
"numberOfOutputs": 4,
"promptIntensity": 0.8,
"refinementStyle": "no_refiner",
"highNoiseFraction": 0.8,
"schedulingAlgorithm": "K_EULER",
"classifierGuidanceScale": 7.5
}
Output
The output of this action typically consists of an array of image URLs pointing to the generated images.
Example Output:
[
"https://assets.cognitiveactions.com/invocations/67ee6780-4ff6-498a-932c-3e17ac69c1e5/d4ca7933-3fe2-4bf8-97a3-504d152460a3.png",
"https://assets.cognitiveactions.com/invocations/67ee6780-4ff6-498a-932c-3e17ac69c1e5/0469908e-8167-4998-9133-fa2ff3fea06b.png",
"https://assets.cognitiveactions.com/invocations/67ee6780-4ff6-498a-932c-3e17ac69c1e5/93cfd002-24ab-4ca5-a20e-2af7cc610826.png",
"https://assets.cognitiveactions.com/invocations/67ee6780-4ff6-498a-932c-3e17ac69c1e5/1b558147-054b-4aee-802b-87c6ae209aae.png"
]
Conceptual Usage Example (Python)
Here’s how you might call the Generate Image with Inpainting 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 = "682826f8-b959-4328-8fe1-5ded5fe1e6ce" # Action ID for Generate Image with Inpainting
# Construct the input payload based on the action's requirements
payload = {
"width": 1024,
"height": 1024,
"prompt": "A sketch digital image of an astronaut in the style of TOK",
"additiveScale": 0.6,
"inferenceSteps": 50,
"invertedPrompt": "background",
"attachWatermark": True,
"numberOfOutputs": 4,
"promptIntensity": 0.8,
"refinementStyle": "no_refiner",
"highNoiseFraction": 0.8,
"schedulingAlgorithm": "K_EULER",
"classifierGuidanceScale": 7.5
}
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, you'll replace the action_id and COGNITIVE_ACTIONS_EXECUTE_URL with the appropriate values. The payload is structured according to the input schema, ensuring that the necessary parameters are included for the action to execute successfully.
Conclusion
The Cognitive Actions offered by the justswim/em API empower developers to create visually compelling images with precision and control. By leveraging these actions, you can enhance your applications with advanced image generation features. Whether you’re building an art application, a marketing tool, or any creative platform, these actions can significantly streamline your workflow. Start exploring these capabilities today and elevate your digital projects!