Create Stunning Images from Text and Visuals with Flux

In an era where visual content is paramount, the ability to generate high-quality images from descriptive text and existing visuals can revolutionize the way developers approach content creation. Flux, powered by the innovative FLUX model from Black Forest Labs, offers a seamless way to create images that align perfectly with your creative vision. With its advanced capabilities, Flux streamlines the image generation process, enabling developers to produce custom visuals quickly and efficiently.
Imagine crafting unique illustrations for marketing campaigns, designing stunning artwork for websites, or generating visuals for social media posts—all with just a few lines of input. With Flux, developers can leverage the power of AI to enhance their projects, saving time and resources while delivering exceptional results.
Prerequisites
To use Flux, you'll need a Cognitive Actions API key and a basic understanding of making API calls.
Generate Image from Text and Image
The "Generate Image from Text and Image" action allows you to create high-quality images based on a specified text prompt and an optional input image. This capability is particularly useful for artists, marketers, and content creators looking to visualize concepts or enhance existing images. The action supports customization options for image attributes, including dimensions, quality, and format, enabling you to tailor outputs to your specific needs.
Input Requirements
The input to this action is structured as a JSON object, which includes several parameters:
- prompt: A descriptive text prompt guiding the image generation.
- image: (Optional) A URI of the input image for image-to-image generation.
- width: The width of the output image in pixels (default is 1024).
- height: The height of the output image in pixels (default is 1024).
- strength: A value from 0 to 1 indicating the degree of alteration from the original image (if using image-to-image mode).
- guidanceScale: A scale factor for guidance during the generation process (default is 3.5).
- numberOfOutputs: The number of images to generate (between 1 and 4).
- imageOutputFormat: The format for the generated images (options include 'webp', 'jpg', or 'png').
- imageOutputQuality: The quality level for the output image (0 to 100).
- numberOfInferenceSteps: The total number of steps for inference (default is 28).
Example Input
{
"width": 1024,
"height": 1024,
"prompt": "a tiny astronaut hatching from an egg on the moon",
"strength": 0.8,
"guidanceScale": 3.5,
"numberOfOutputs": 1,
"imageOutputFormat": "png",
"imageOutputQuality": 100,
"numberOfInferenceSteps": 28
}
Expected Output
The expected output is a URL to the generated image, for example:
"https://assets.cognitiveactions.com/invocations/852a4db8-32f4-425d-95a8-5b78a3a4a631/c786a8c3-9243-4ae5-aebb-2e16aea098fe.png"
Use Cases for this Action
- Marketing Campaigns: Generate eye-catching visuals tailored to specific campaigns, enhancing engagement and communication.
- Social Media Content: Quickly create unique images that stand out on platforms like Instagram and Facebook, helping brands maintain a fresh aesthetic.
- Concept Visualization: Transform creative ideas into visual representations for pitches or presentations, making it easier to convey messages and concepts.
- Artistic Exploration: Artists can experiment with different styles and compositions by generating variations of a base image or concept.
```python
import requests
import json
# Replace with your actual Cognitive Actions API key and endpoint
# Ensure your environment securely handles the API key
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
# This endpoint URL is hypothetical and should be documented for users
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"
action_id = "7b3a3fae-a507-4181-b142-fcd80bb733a8" # Action ID for: Generate Image from Text and Image
# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
"width": 1024,
"height": 1024,
"prompt": "a tiny astronaut hatching from an egg on the moon",
"strength": 0.8,
"guidanceScale": 3.5,
"numberOfOutputs": 1,
"imageOutputFormat": "png",
"imageOutputQuality": 100,
"numberOfInferenceSteps": 28
}
headers = {
"Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
"Content-Type": "application/json",
# Add any other required headers for the Cognitive Actions API
}
# Prepare the request body for the hypothetical execution endpoint
request_body = {
"action_id": action_id,
"inputs": payload
}
print(f"--- Calling Cognitive Action: {action.name or action_id} ---")
print(f"Endpoint: {COGNITIVE_ACTIONS_EXECUTE_URL}")
print(f"Action ID: {action_id}")
print("Payload being sent:")
print(json.dumps(request_body, indent=2))
print("------------------------------------------------")
try:
response = requests.post(
COGNITIVE_ACTIONS_EXECUTE_URL,
headers=headers,
json=request_body
)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
result = response.json()
print("Action executed successfully. Result:")
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 (non-JSON): {e.response.text}")
print("------------------------------------------------")
## Conclusion
Flux empowers developers to create stunning visual content effortlessly by transforming text prompts and existing images into high-quality outputs. With a range of customization options, this action opens the door to endless creative possibilities. Whether for marketing, social media, or artistic endeavors, leveraging Flux can significantly enhance your projects and streamline your workflow. Start integrating Flux into your applications today, and unlock the potential of AI-driven image generation!