Image Saving
save_image() writes a numpy array directly to PNG or JPEG — no manual conversion needed. Useful for MuJoCo/PyBullet renders, RL observations, model predictions, or any HxW / HxWxC array.
Requires Pillow: pip install Pillow.
Basic Usage
save() auto-detects numpy arrays and dispatches to save_image(), so these are equivalent:
Works with any numpy array — OpenCV frames (remember cv2.cvtColor(..., COLOR_BGR2RGB)), np.array(PIL.Image), model outputs, etc.
Array Types
- uint8 — passed through directly. Shape
HxW(grayscale),HxWx3(RGB), orHxWx4(RGBA). - float in
[0.0, 1.0]— multiplied by 255 and cast to uint8. - float in any other range — normalized via
(value - min) / (max - min) * 255.
Format: PNG vs JPEG
| 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 |
The extension picks the encoder: .png, .jpg, and .jpeg all work. JPEG drops the alpha channel (transparent pixels composited onto white) and applies optimization.
Quality
JPEG only. Default is 95. Range 1–100.
Rough guide: 95–100 near-lossless, 85–90 balanced (recommended default for sequences), 70–80 visible compression, below 50 poor.
API Reference
save_image(array, *, to, quality=95)
Save a numpy array as an image file.
Parameters
array(numpy.ndarray) — image array, shapeHxWorHxWxC.to(str) — target filename with extension (.png,.jpg,.jpeg).quality(int, optional) — JPEG quality 1–100. Default 95. Ignored for PNG.
Returns — dict with file metadata, or a queued-status dict when the write is buffered (see /buffering).
Raises — ImportError if Pillow is missing; ValueError for invalid array or missing to.
Example: MuJoCo Renders
Image writes are buffered for non-blocking uploads — see /buffering. To align frames with numeric data, share a step index across save_image() and track() calls — see /tracks.