Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ollama Ollama Imagegen Image

From Leeroopedia
Knowledge Sources
Domains Image Generation, Image Processing
Last Updated 2025-02-15 00:00 GMT

Overview

Provides image conversion utilities for MLX arrays, including saving to PNG, base64 encoding, and EXIF-aware image decoding.

Description

The image.go file provides bidirectional conversion between MLX arrays and Go image types. SaveImage and EncodeImageBase64 convert [B, C, H, W] MLX arrays to PNG format by squeezing the batch dimension, transposing to [H, W, C], clamping values to [0, 255], and writing directly to pixel buffers. ArrayToImage handles the core conversion with proper memory management (freeing intermediate arrays). DecodeImage reads image bytes with EXIF orientation correction (for JPEG) and alpha channel flattening (compositing onto white background) to prepare inputs for image generation models. The file provides a complete pipeline from raw bytes to model-ready tensors and back.

Usage

Used throughout the imagegen subsystem to convert generated MLX arrays to image files or base64 strings for API responses, and to decode input images for image-to-image workflows.

Code Reference

Source Location

  • Repository: Ollama
  • File: x/imagegen/image.go
  • Lines: 1-295

Signature

func SaveImage(arr *mlx.Array, path string) error
func EncodeImageBase64(arr *mlx.Array) (string, error)
func ArrayToImage(arr *mlx.Array) (*image.RGBA, error)
func DecodeImage(data []byte) (image.Image, error)

Import

import "github.com/ollama/ollama/x/imagegen"

I/O Contract

Inputs

Name Type Required Description
arr *mlx.Array Yes Image tensor [B, C, H, W] with values in [0, 1]
path string Yes Output file path for SaveImage
data []byte Yes Raw image bytes for DecodeImage

Outputs

Name Type Description
*image.RGBA *image.RGBA Converted Go RGBA image
string string Base64-encoded PNG string
error error Conversion or I/O error

Usage Examples

// Save generated image to file
err := imagegen.SaveImage(generatedArray, "output.png")

// Encode for API response
base64Str, err := imagegen.EncodeImageBase64(generatedArray)

// Decode input image with EXIF correction
img, err := imagegen.DecodeImage(imageBytes)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment