Conversion & Display Utilities
Conversion & Display Utilities
Udility provides built-in utility functions to handle the conversion of generated SVG code into standard image formats and to visualize the results directly within your development environment.
svg_to_png
This utility converts a raw SVG string into a PNG image file. It is particularly useful when you need to programmatically save high-quality raster versions of the generated illustrations for use in documents or web applications.
Syntax:
diffuser.svg_to_png(svg_code, output_filename='output.png')
Parameters:
| Parameter | Type | Description |
| :--- | :--- | :--- |
| svg_code | str | The raw SVG XML string generated by the model. |
| output_filename | str | (Optional) The name/path of the file to save. Defaults to output.png. |
Example:
from Udility import diffuser
svg_data = "<svg>...</svg>" # SVG code obtained from the generator
diffuser.svg_to_png(svg_data, "my_illustration.png")
display_image
This utility renders a PNG file within your interface. It leverages matplotlib and Pillow to display images, making it ideal for users working in Jupyter Notebooks, Google Colab, or other interactive Python environments.
Syntax:
diffuser.display_image(image_path)
Parameters:
| Parameter | Type | Description |
| :--- | :--- | :--- |
| image_path | str | The local path to the PNG image file you wish to view. |
Example:
from Udility import diffuser
# Display a previously generated PNG
diffuser.display_image("output.png")
Combined Workflow Example
While generate_image_from_text handles these steps automatically, you can use these utilities individually for more granular control over the generation pipeline:
from Udility import diffuser
# 1. Manually generate instructions and SVG code
instructions = diffuser.get_detailed_instructions("A diagram of a water molecule")
svg_code = diffuser.generate_svg_from_instructions(instructions)
# 2. Convert the SVG to a PNG file
filename = "water_molecule.png"
diffuser.svg_to_png(svg_code, filename)
# 3. Display the resulting file
diffuser.display_image(filename)