siyuan-note/siyuan · error

encode image failed:

Error message

encode image failed: 

What it means

For PNG/WebP sources (or any case that reaches the PNG encode path) PrepareModelImage re-encodes via png.Encode after resize. Encoder failure is very rare and indicates a degenerate decoded image (e.g. zero-area frame from a malformed GIF) or memory pressure.

Source

Thrown at kernel/util/openai.go:667

	bounds := decoded.Bounds()
	needsResize := maxEdge > 0 && (bounds.Dx() > maxEdge || bounds.Dy() > maxEdge)
	if !needsResize && bounds.Dx() == config.Width && bounds.Dy() == config.Height && mimeType != "image/gif" {
		return PreparedImage{
			Data:       data,
			MIMEType:   mimeType,
			Width:      bounds.Dx(),
			Height:     bounds.Dy(),
			SourceSize: len(data),
		}, nil
	}
	if needsResize {
		decoded = imaging.Fit(decoded, maxEdge, maxEdge, imaging.Lanczos)
	}
	bounds = decoded.Bounds()
	if mimeType == "image/png" || mimeType == "image/webp" {
		var output bytes.Buffer
		if err = png.Encode(&output, decoded); err != nil {
			return PreparedImage{}, errors.New("encode image failed: " + err.Error())
		}
		if maxBytes <= 0 || output.Len() <= maxBytes {
			return PreparedImage{
				Data:       output.Bytes(),
				MIMEType:   "image/png",
				Width:      bounds.Dx(),
				Height:     bounds.Dy(),
				SourceSize: len(data),
			}, nil
		}
	}
	var output bytes.Buffer
	if err = jpeg.Encode(&output, decoded, &jpeg.Options{Quality: 92}); err != nil {
		return PreparedImage{}, errors.New("encode image failed: " + err.Error())
	}
	if maxBytes > 0 && output.Len() > maxBytes {
		return PreparedImage{}, fmt.Errorf("prepared image exceeds size limit: %d bytes", maxBytes)
	}

View on GitHub (pinned to 251596fc0d)

Solutions

  1. Re-save the source as a clean PNG/JPEG.
  2. For a GIF, extract a single representative frame as PNG instead of feeding the whole GIF.
  3. Reduce input resolution to relieve memory.
Defensive patterns

Strategy: try-catch

Try / catch

if _, err := util.PrepareModelImage(data, maxB, maxP, maxE); err != nil {
    log.Warnf("drop unencodable image: %s", err)
    continue
}

Prevention

When it happens

Trigger: Pathological GIF frame decoded to a degenerate image; memory pressure during encode; an unusual decoded image the PNG encoder cannot handle.

Common situations: Malformed animated GIF whose frame is zero-area; very large frame under memory pressure.

Related errors


AI-assisted analysis of siyuan-note/siyuan@251596fc0d (2026-08-12). Data as JSON: /api/errors/df43eb163c411d5f. Report an issue: GitHub.