Image Saving
ML-Dash provides direct support for saving numpy arrays as images in PNG and JPEG formats.
Overview
The save_image() method allows you to save numpy arrays directly without manually converting to image formats. This is perfect for:
- MuJoCo/PyBullet: Rendering frames from physics simulations
- Computer Vision: Saving model predictions, visualizations
- Reinforcement Learning: Saving agent observations
- Any Numpy Arrays: Camera feeds, generated images, etc.
Basic Usage
Auto-Detection
The save() method automatically detects numpy arrays:
Supported Array Types
1. uint8 Arrays (Most Common)
2. Normalized Float Arrays (0.0 to 1.0)
3. Arbitrary Range Float Arrays
Format Support
PNG (Lossless, Larger Files)
JPEG (Lossy, Smaller Files)
JPEG Important Notes:
- Automatically converts RGBA to RGB (alpha channel removed)
- Uses white background when converting transparent images
- Applies optimization for better compression
Quality Parameter
Control JPEG compression quality (default: 95):
Quality Guidelines:
- 95-100: Nearly lossless, large files
- 85-90: Great for most use cases, good balance
- 70-80: Visible compression, smaller files
- 50-60: Noticeable artifacts, very small files
- Below 50: Poor quality, not recommended
MuJoCo Example
OpenCV Example
PIL/Pillow Integration
Saving Image Sequences
Buffering and Performance
Image saves are automatically buffered:
Aligning with Tracks
Use consistent step indices:
Different Paths
Organize images in different directories:
Error Handling
Format Comparison
| Aspect | PNG | JPEG |
|---|---|---|
| Compression | Lossless | Lossy |
| File Size | Larger | Smaller |
| Transparency | ✓ Yes | ✗ No |
| Quality | Perfect | Configurable |
| Best For | Graphics, text | Photos, renders |
| Speed | Slower | Faster |
Best Practices
1. Choose Format Based on Content
2. Use Quality 85 for Balanced JPEG
3. Zero-Pad Frame Numbers
4. Consistent File Extensions
5. Include Frame Info in Metadata
Requirements
Image saving needs Pillow and NumPy. Neither is installed with ml-dash:
API Reference
save_image(array, *, to, quality=95)
Save numpy array as an image file.
Parameters:
array(numpy.ndarray): Image array (HxW or HxWxC)to(str): Target filename with extension (.png, .jpg, .jpeg)quality(int, optional): JPEG quality 1-100 (default: 95)
Returns:
- Dict with file metadata (or
{"status": "queued"}if buffered)
Raises:
ImportError: If Pillow not installedValueError: Iftoparameter missing or invalid array
Examples:
Complete Example
This creates a complete dataset with rendered frames and aligned position tracking!