IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /max/get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).

Python function

hash_image

hash_image()

max.support.image.hash_image(image, size_tier=None)

source

Compute the hash of an image.

Two input modes are supported:

  • Raw encoded bytes (preferred for prefix-cache / routing keys): pass the raw encoded image container bytes (the JPEG/PNG/WebP bytes as received by the router, before any decode or resize) as image, together with a size_tier capturing the resolution class the image is processed at. Because it never touches decoded float pixels, the result is byte-identical across torch/numpy/BLAS versions and CPU-vs-GPU, so it is safe as a key that a separate encoder must reproduce. This mirrors that encoder’s content_key: xxh3_64(bytes ++ size_tier) with size_tier appended as a little-endian unsigned 64-bit integer.
  • Decoded pixels (legacy): pass a numpy pixel array as image and omit size_tier. Supports any dtype (float32, uint16 for bfloat16 bits, etc.) and ensures a C-contiguous layout before hashing. The digest depends on the post-resize float pixels, so it is not reproducible across torch/BLAS/device; prefer the raw-bytes mode where the encoded bytes are available.

In both modes the unsigned 64-bit digest is reinterpreted as a signed 64-bit int for numpy int64 token-hash compatibility.

Parameters:

  • image (ndarray[tuple[Any, ...], dtype[Any]] | bytes) – Either the raw encoded image bytes or a decoded numpy pixel array.
  • size_tier (int | None) – The resolution size class folded into the key, so identical bytes processed at different resolutions key distinctly. Required when image is bytes; must be omitted for a pixel array.

Returns:

The signed 64-bit image hash.

Raises:

  • ValueError – If image is bytes and size_tier is not provided, or if image is a pixel array and size_tier is provided (the tier is only folded into the raw-bytes key, so passing it with a pixel array is a silent no-op we reject rather than swallow).
  • OverflowError – If size_tier is negative or does not fit in an unsigned 64-bit integer, since the key appends it as a little-endian u64.

Return type:

int