Skip to content

Bambu G-code Thumbnail Support

This feature adds support for displaying thumbnails and metadata from Bambu Lab slicer generated G-code files in Printernizer.

Features

Thumbnail Extraction

  • Extracts PNG thumbnails embedded in G-code file comments
  • Supports multiple thumbnail sizes (typically 220x124 and 64x64)
  • Falls back to file type icons when thumbnails are not available
  • Automatic processing when files are downloaded from printers

Metadata Parsing

The parser extracts the following metadata from Bambu G-code files:

Print Settings: - Layer height and first layer height - Infill density percentage - Print speeds (outer wall speed) - Support material usage (true/false)

Temperature Settings: - Nozzle temperature (initial layer) - Bed temperature (initial layer)

Print Information: - Total layer count - Estimated print time (parsed to seconds) - Total filament usage (in grams) - Filament cost estimation - Filament type (PLA, PETG, etc.) - AMS slot assignments

API Endpoints

Get File Thumbnail

GET /api/files/{file_id}/thumbnail
Returns the thumbnail image for a file (PNG format) with proper caching headers.

Process Thumbnails Manually

POST /api/files/{file_id}/process-thumbnails
Manually trigger thumbnail processing for a downloaded file.

Frontend Display

File Cards

Files with thumbnails will display: - Thumbnail image (48x48 pixels with fallback to file icons) - Parsed metadata as colored badges: - โฑ๏ธ Print time estimation - ๐Ÿ“ Layer count - ๐Ÿงต Filament usage - ๐Ÿ“ Layer height - ๐Ÿ—๏ธ Infill percentage

Responsive Design

  • Thumbnails are responsive and work on mobile devices
  • Graceful fallback when thumbnails fail to load
  • Loading states and error handling

Database Schema

The feature adds the following columns to the files table:

ALTER TABLE files ADD COLUMN has_thumbnail BOOLEAN DEFAULT 0 NOT NULL;
ALTER TABLE files ADD COLUMN thumbnail_data TEXT; -- Base64 encoded
ALTER TABLE files ADD COLUMN thumbnail_width INTEGER;
ALTER TABLE files ADD COLUMN thumbnail_height INTEGER;
ALTER TABLE files ADD COLUMN thumbnail_format TEXT;

Configuration

New configuration options in the system configuration:

  • thumbnails.enabled - Enable/disable thumbnail processing
  • thumbnails.max_size_kb - Maximum thumbnail size (500KB default)
  • thumbnails.preferred_width - Preferred thumbnail width (200px default)
  • thumbnails.preferred_height - Preferred thumbnail height (200px default)
  • thumbnails.cache_lifetime_hours - Thumbnail cache duration (24h default)

Supported File Types

Currently supports: - G-code files (.gcode, .g) with Bambu Lab comment format - 3MF files with embedded thumbnails (basic support)

Usage Examples

Automatic Processing

When files are downloaded from printers, thumbnails are automatically processed:

# File service automatically processes thumbnails
success = await file_service.download_file(printer_id, filename, destination_path)
# Thumbnails are processed asynchronously after download

Manual Processing

For existing files, you can trigger processing manually:

# Process thumbnails for a specific file
success = await file_service.process_file_thumbnails(file_path, file_id)

Accessing Thumbnails in Frontend

// Check if file has thumbnail
if (file.has_thumbnail) {
    const thumbnailUrl = `/api/files/${file.id}/thumbnail`;
    // Use thumbnailUrl in <img> src
}

// Access parsed metadata
if (file.metadata) {
    const printTime = file.metadata.estimated_time; // in seconds
    const filamentUsage = file.metadata.total_filament_used; // in grams
    const layerHeight = file.metadata.layer_height; // in mm
}

Sample G-code Format

The parser recognizes this Bambu Lab format:

; generated by BambuStudio 1.7.7.0
; layer_height = 0.2
; nozzle_temperature_initial_layer = 220
; bed_temperature_initial_layer = 60
; estimated printing time (normal mode) = 2h 30m 15s
; filament used [g] = 25.5
; total layer count = 250

; thumbnail begin 220x124 6432
; /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEB...
; thumbnail end

Performance Considerations

  • Thumbnails are processed asynchronously to avoid blocking downloads
  • Base64 thumbnail data is cached in the database
  • HTTP responses include proper caching headers (24-hour cache)
  • Large thumbnails are automatically resized to preferred dimensions
  • Failed thumbnail processing doesn't block file operations

Error Handling

  • Invalid base64 data is gracefully ignored
  • Missing thumbnail sections don't cause parsing failures
  • Unsupported file types return appropriate error messages
  • Frontend displays fallback icons when thumbnails fail to load

Testing

Run the thumbnail parser tests:

pytest tests/test_bambu_parser.py -v

The test suite covers: - G-code parsing with various metadata formats - Thumbnail extraction and validation - Duration parsing (2h 30m 15s format) - Base64 validation - Error handling for invalid files