imageproc

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package imageproc normalizes an image once before it enters the conversation history: it detects the format, converts it to a format models accept, and limits its dimensions and size. The framework never calls it; the caller decides which images need it.

An oversized image makes the server reject every later request in the session, not just the failing turn, and modifying an image already in history breaks the prompt cache, so process images when they first enter history and never change them afterwards.

Index

Constants

View Source
const (
	MIMEPNG  = "image/png"
	MIMEJPEG = "image/jpeg"
	MIMEGIF  = "image/gif"
	MIMEWebP = "image/webp"
)

Inline image formats accepted by all providers; other formats (such as BMP) are converted to PNG.

Variables

View Source
var (
	// ErrUnsupportedFormat means the data cannot be recognized or decoded as an image.
	ErrUnsupportedFormat = errors.New("imageproc: unsupported image format")
	// ErrTooLarge means the image cannot be compressed within the limits.
	ErrTooLarge = errors.New("imageproc: image cannot be compressed within the limits")
)

Functions

func DetectMIME

func DetectMIME(data []byte) string

DetectMIME detects the image format from its magic bytes, ignoring the extension and any declared MIME; it returns "" when unrecognized.

func Fit

func Fit(data []byte, model chat.ModelInfo) ([]chat.ContentBlock, error)

Fit processes an image for the model's image specs and returns content blocks ready for a message: the image block, plus a note when the image was converted or shrunk.

Types

type Result

type Result struct {
	Data     []byte
	MIMEType string
	Width    int
	Height   int
	// Notes are remarks the model should know, for example that coordinates must be rescaled after the image was shrunk.
	Notes []string
}

Result is a normalized image.

func Process

func Process(data []byte, limits chat.ImageLimits) (Result, error)

Process normalizes an image within limits. Zero fields of limits use chat.DefaultImageLimits.

PNG, JPEG, GIF and WebP images within the dimension and size limits are returned unchanged; other formats become PNG; oversized images are scaled down proportionally and encoded as the smaller of PNG and JPEG, lowering quality and size when needed.

func (Result) Block

func (r Result) Block() chat.ContentBlock

Block returns the image block to put in a chat.Message.

func (Result) Blocks

func (r Result) Blocks() []chat.ContentBlock

Blocks returns the image block, followed by a text block when there are notes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL