Image Display and Plotting
Image Display and Plotting
Udility Diffuser provides built-in utilities for visualizing generated illustrations directly within your Python environment. By leveraging Matplotlib for plotting and Pillow (PIL) for image processing, the library ensures that images are rendered cleanly without axis labels or unnecessary metadata.
Automatic Visualization
The primary way to generate and view images is via the generate_image_from_text function. This function handles the entire pipeline: converting text to SVG, rendering that SVG to a PNG file, and automatically plotting it to the screen.
from Udility import diffuser
# Generates 'displacement_chart.png' and displays it immediately
diffuser.generate_image_from_text(
"A diagram showing displacement vs distance",
output_filename="displacement_chart.png"
)
Manual Image Display
If you have an existing image file or wish to re-display a previously generated image, you can use the display_image utility. This function uses Matplotlib to create a plot frame and Pillow to load the image data.
| Parameter | Type | Description |
| :--- | :--- | :--- |
| image_path | str | The file path to the PNG image you wish to display. |
Example:
from Udility import diffuser
# Display a previously saved image
diffuser.display_image("output.png")
File and Buffer Management
Udility Diffuser manages the transition from vector graphics (SVG) to raster images (PNG) using a combination of cairosvg and file-system writes.
- SVG Rendering: The library uses
cairosvgto transform raw SVG strings into PNG byte streams. - File Persistence: By default, images are saved to the local directory as
output.pngunless a customoutput_filenameis provided. - Matplotlib Integration: The plotting engine calls
plt.imshow()andplt.axis('off')to ensure the focus remains entirely on the illustration, making it ideal for Jupyter Notebooks and Google Colab environments.
Technical Dependencies
To ensure smooth plotting and display, the following libraries are utilized under the hood:
- Pillow: Used for opening and verifying image files.
- Matplotlib: Used for rendering the image within a graphical window or notebook cell.
- CairoSVG: Handles the conversion of generated SVG code into a renderable PNG format.