Create Stunning Handwritten Text Images with the fofr/flux-handwriting Cognitive Actions

In the ever-evolving world of image generation, the fofr/flux-handwriting API offers a powerful tool for developers looking to create realistic handwritten text images. Utilizing a fine-tuned flux LORA model, this API enables you to generate images with customizable settings, allowing for unique and personalized outputs. Whether you're building applications for educational purposes, digital art, or marketing, these pre-built actions simplify the process of adding handwritten text to your projects.
Prerequisites
Before diving into the integration of the Cognitive Actions, ensure you have the following:
- API Key: Access to the Cognitive Actions platform requires an API key. This key should be included in the request headers for authentication.
- Basic Understanding of JSON: Familiarity with JSON structure will help you in constructing the input payload for the API requests.
Cognitive Actions Overview
Generate Handwritten Text Image
The Generate Handwritten Text Image action allows developers to create images featuring handwritten text based on a specified prompt. You can customize various parameters such as aspect ratio, image quality, and output format for tailored image generation.
Input
The action accepts the following input parameters:
- prompt (required): A string that describes the handwritten text you want to generate.
- mask (optional): A URI for an image mask used in inpainting mode.
- seed (optional): An integer for setting a random seed to ensure consistent outputs.
- image (optional): A URI for an input image for image-to-image or inpainting mode.
- width (optional): An integer specifying the width of the generated image (must be between 256 and 1440).
- height (optional): An integer specifying the height of the generated image (must be between 256 and 1440).
- fastMode (optional): A boolean that toggles speed-optimized model predictions.
- resolution (optional): A string indicating the image resolution (1 or 0.25).
- imageFormat (optional): The output file format (webp, jpg, png).
- outputCount (optional): An integer specifying how many images to generate (1 to 4).
- imageQuality (optional): An integer indicating the quality level of the output images (0 to 100).
- loraIntensity (optional): A number determining the intensity of the LORA model application.
- inferenceModel (optional): A string indicating which inference model to use (dev or schnell).
- promptIntensity (optional): A number that determines how strongly the prompt influences the results.
- imageAspectRatio (optional): A string for selecting the output image's aspect ratio.
- safetyCheckerOff (optional): A boolean option to disable the safety checker.
- diffusionGuideScale (optional): A number that sets the guidance scale for diffusion.
- inferenceStepsCount (optional): An integer specifying the number of steps in denoising.
- additionalLoraWeights (optional): URIs for loading additional LORA model weights.
Example Input:
{
"prompt": "HWRIT handwriting saying \"Hello, this is a handrwriting lora\", messy style, blue ink on paper",
"fastMode": false,
"resolution": "1",
"imageFormat": "jpg",
"outputCount": 1,
"imageQuality": 80,
"loraIntensity": 1,
"inferenceModel": "dev",
"promptIntensity": 0.8,
"imageAspectRatio": "1:1",
"diffusionGuideScale": 3,
"inferenceStepsCount": 28,
"additionalLoraIntensity": 1
}
Output
Upon successful execution, the action returns a URL pointing to the generated handwritten text image. The output may look as follows:
Example Output:
[
"https://assets.cognitiveactions.com/invocations/7a3d24cb-fdb0-4d4b-afc7-e45e66fab453/e534ea16-9ab3-4ebe-9073-a81ee3d2127f.jpg"
]
Conceptual Usage Example (Python)
Here’s how you can use the Generate Handwritten Text Image action in 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 = "cffc6c17-31c0-45b3-abe9-27c870f252b7" # Action ID for Generate Handwritten Text Image
# Construct the input payload based on the action's requirements
payload = {
"prompt": "HWRIT handwriting saying \"Hello, this is a handrwriting lora\", messy style, blue ink on paper",
"fastMode": False,
"resolution": "1",
"imageFormat": "jpg",
"outputCount": 1,
"imageQuality": 80,
"loraIntensity": 1,
"inferenceModel": "dev",
"promptIntensity": 0.8,
"imageAspectRatio": "1:1",
"diffusionGuideScale": 3,
"inferenceStepsCount": 28,
"additionalLoraIntensity": 1
}
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 example, replace YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key. The payload variable is constructed based on the input schema for the action, and the request is sent to the hypothetical endpoint. The printed result will show the URL of the generated image.
Conclusion
The fofr/flux-handwriting Cognitive Actions provide a versatile and powerful way to create handwritten text images that can enhance various applications. By integrating these actions, you can easily generate customized images with different styles, qualities, and formats. Consider exploring other potential use cases, such as integrating this functionality into educational tools, creative applications, or marketing materials to further enrich user experiences. Happy coding!