Transform Your Images with Prompt-to-Prompt Editing Using Cognitive Actions

Cognitive Actions provide a powerful set of tools for developers looking to enhance their applications with advanced image processing capabilities. In particular, the Prompt-to-Prompt actions enable you to edit images generated by Stable Diffusion by modifying the prompts that created them. This allows for refined control over image outputs, making it easier to achieve the desired aesthetic or thematic results with minimal effort.
Prerequisites
Before diving into the capabilities of the Prompt-to-Prompt actions, ensure you have the following:
- An API key for the Cognitive Actions platform.
- Familiarity with sending HTTP requests and handling JSON data.
- Basic knowledge of image processing concepts and how prompts influence image generation.
Typically, authentication is implemented by passing your API key in the request headers when invoking actions.
Cognitive Actions Overview
Edit Image with Prompt-to-Prompt
The Edit Image with Prompt-to-Prompt action allows you to modify images generated by Stable Diffusion by adjusting the original prompt. You can refine, replace, or re-weight specific parts of the prompt to achieve different artistic outcomes.
Input: This action requires the following input fields:
- originalPrompt (string, required): The initial prompt used to generate the image.
Example: "A painting of a squirrel eating a burger" - editedPrompt (string, required when
promptEditTypeis set): The modified prompt that will be used for editing.
Example: "A painting of a lion eating a burger" - promptEditType (string, required): The type of editing to apply. Options are "Replacement", "Refinement", or "Re-weight".
Example: "Replacement" - seed (integer, optional): A random seed for consistent output. You can leave this empty for a unique image.
Example: 8888 - localEdit (string, optional): Words for local editing specified in the format 'words_in_original_prompt | words_in_edited_prompt'.
- selfReplaceSteps (number, optional): Fraction for self-attention replacement, ranging from 0 to 1. Default is 0.4.
Example: 0.4 - crossReplaceSteps (number, optional): Fraction for cross-attention replacement, ranging from 0 to 1. Default is 0.8.
Example: 0.8
Example Input:
{
"seed": 8888,
"editedPrompt": "A painting of a lion eating a burger",
"originalPrompt": "A painting of a squirrel eating a burger",
"promptEditType": "Replacement",
"selfReplaceSteps": 0.4,
"crossReplaceSteps": 0.8
}
Output: The action returns the following output, which includes URLs to the original and edited images:
- original_sd (string): URL of the image generated from the original prompt.
- with_prompt_to_prompt (string): URL of the edited image based on the modified prompt.
Example Output:
{
"original_sd": "https://assets.cognitiveactions.com/invocations/39ed6188-2d0b-40bf-808d-af49f0bbfa45/9c33f89a-b3a1-4e07-8ea1-c76eeea63e3d.png",
"with_prompt_to_prompt": "https://assets.cognitiveactions.com/invocations/39ed6188-2d0b-40bf-808d-af49f0bbfa45/770af87c-b211-4dc4-9bde-ab697017a4ba.png"
}
Conceptual Usage Example (Python): Here’s how you might invoke the Edit Image with Prompt-to-Prompt 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 = "c474cac0-a265-4257-92af-7cda4f2ff49b" # Action ID for Edit Image with Prompt-to-Prompt
# Construct the input payload based on the action's requirements
payload = {
"seed": 8888,
"editedPrompt": "A painting of a lion eating a burger",
"originalPrompt": "A painting of a squirrel eating a burger",
"promptEditType": "Replacement",
"selfReplaceSteps": 0.4,
"crossReplaceSteps": 0.8
}
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 payload contains the input fields required for the action, and the response from the API will include links to both the original and edited images.
Conclusion
The Prompt-to-Prompt Cognitive Actions provide an efficient way to refine and enhance images generated by Stable Diffusion. By leveraging the ability to modify prompts directly, developers can bring their creative visions to life with ease. Consider exploring different edit types and settings to discover the full potential of this action in your applications.