Main Image Generation API
Main Image Generation API
The primary interface for creating images in Udility Diffuser is the diffuser module. It abstracts the complex process of instruction generation, SVG scripting, and rasterization into a single, user-friendly function.
generate_image_from_text
This function is the main entry point for generating labeled illustrations. It takes a natural language description, processes it through the Meta Llama-based engine to create an SVG, converts that SVG into a high-quality PNG, and displays the result.
Syntax
Udility.diffuser.generate_image_from_text(text_description, output_filename='output.png')
Parameters
| Parameter | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| text_description | str | Required | A detailed text prompt describing the educational or illustrative image you wish to generate. |
| output_filename | str | 'output.png' | The file path and name where the resulting PNG image will be saved. |
Returns
- None: The function saves the image to the local file system and renders the output directly in your IDE or notebook environment using
matplotlib.
Usage Examples
The following examples demonstrate how to use the API for various educational and scientific visualizations.
1. Basic Scientific Illustration
Generate a conceptual diagram by providing a clear descriptive prompt.
from Udility import diffuser
# Generate and save an image explaining a biological process
diffuser.generate_image_from_text(
"A detailed diagram showing the lifecycle of an amoeba with labels.",
output_filename="amoeba_lifecycle.png"
)
2. Mathematical Visualization
Visualize complex mathematical concepts or functions.
from Udility import diffuser
# Visualize calculus concepts
diffuser.generate_image_from_text(
"A graph illustrating the area under a curve to explain definite integration.",
output_filename="integration_viz.png"
)
3. Physics Concepts
Create labeled diagrams to differentiate between related physical quantities.
from Udility import diffuser
# Compare distance and displacement
diffuser.generate_image_from_text("Show the difference between distance and displacement using a vector path.")
Internal Workflow Components
While generate_image_from_text is the recommended path for most users, the diffuser module utilizes several internal helper functions to process the request. These are available for advanced workflows but generally do not need to be called manually:
get_detailed_instructions(text_description): Interacts with the LLM to convert a user prompt into a structured technical plan for an SVG.generate_svg_from_instructions(detailed_instructions): Takes the technical plan and generates the raw SVG source code.svg_to_png(svg_code, output_filename): A utility that leveragescairosvgto convert the vector SVG code into a portable PNG file.display_image(image_path): UsesmatplotlibandPILto render the final image in the console or notebook.
Requirements for Execution
To ensure the API functions correctly, verify the following:
- API Key: The
OPENROUTER_API_KEYmust be set in your environment variables. - Dependencies: Ensure
cairosvgandmatplotlibare correctly configured in your Python environment, as these handle the visual rendering and conversion.