Processing Utilities
Processing Utilities
The Udility library provides a set of utility functions within the diffuser module to handle the conversion, rendering, and visualization of generated SVG data. While the high-level generate_image_from_text function handles the entire pipeline, these utilities allow for granular control over the output.
Image Conversion
svg_to_png
This utility converts raw SVG code (generated by the model) into a high-quality PNG image file. It handles the rasterization process using the cairosvg engine.
Parameters:
svg_code(str): The raw XML string of the SVG image.output_filename(str, optional): The desired file path for the output image. Defaults to'output.png'.
Example Usage:
from Udility import diffuser
svg_data = '<svg width="100" height="100">...</svg>'
diffuser.svg_to_png(svg_data, output_filename='my_illustration.png')
Visualization
display_image
A helper function designed primarily for users working in interactive environments (like Jupyter Notebooks or Google Colab). It uses matplotlib to render a PNG file directly in the output cell.
Parameters:
image_path(str): The local path to the PNG image file that should be displayed.
Example Usage:
from Udility import diffuser
# Display an existing image file
diffuser.display_image('output.png')
Core Pipeline Utilities
While typically orchestrated by the main generate_image_from_text command, these functions can be used independently to interact with the LLM-to-SVG workflow.
get_detailed_instructions
Interacts with the Meta Llama-3.5 model to translate a simple user prompt into a structured set of visual instructions.
- Input:
text_description(str) - Returns:
detailed_instructions(str)
generate_svg_from_instructions
Takes the structured instructions and generates valid SVG code.
- Input:
detailed_instructions(str) - Returns:
svg_code(str)
Advanced Workflow Example:
from Udility import diffuser
# 1. Get visual plan
instructions = diffuser.get_detailed_instructions("A diagram of a plant cell")
# 2. Generate SVG code from plan
svg_code = diffuser.generate_svg_from_instructions(instructions)
# 3. Save as PNG
diffuser.svg_to_png(svg_code, "plant_cell.png")
# 4. Show result
diffuser.display_image("plant_cell.png")
Error Handling
All processing utilities are wrapped in error handling blocks. If an API connection fails or a conversion error occurs (e.g., malformed SVG), the functions will raise a RuntimeError with a descriptive message to assist in debugging.