Skip to content

[L6] Inline preview encoding (Pillow downscale → WebP) #6

Description

@Seretos

#ai-generated

Summary

Produce a small, context-budget-friendly preview of an image asset for inline display in an MCP tool result: downscale with Pillow and encode to WebP under a size cap, with a graceful "too large → metadata only" fallback.

Footprint (new/changed files)

  • src/lib_python_comfy/preview.py:
    • encode_preview(image_bytes, *, max_dim=512, max_b64_chars=100_000, quality=70) -> PreviewResult (raw WebP bytes + base64 + whether it fit the budget)
  • tests/test_preview.py — uses a generated in-memory test image (no network).

Acceptance criteria

  • Output respects max_dim and stays under max_b64_chars; quality steps down if needed.
  • Non-image / undecodable input fails cleanly with a clear error (caller can fall back to metadata).
  • Pure function over bytes — no client/network dependency.

Dependencies

  • Must come after L1 (Pillow dep) and L5 (operates on asset bytes / Asset). blocked_by set for both.

Notes

Mirrors encode_preview_for_mcp / view_image in the reference. The MCP-side view_image tool (wrapper ticket A4) calls this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai-generatedCreated by the project-issues AI agentenhancementNew feature or requestmilestone:M1Milestone 1 — media-agnostic engine

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions