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)
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 asize_tiercapturing 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’scontent_key:xxh3_64(bytes ++ size_tier)withsize_tierappended as a little-endian unsigned 64-bit integer. - Decoded pixels (legacy): pass a numpy pixel array as
imageand omitsize_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
imageis bytes; must be omitted for a pixel array.
-
Returns:
-
The signed 64-bit image hash.
-
Raises:
-
- ValueError – If
imageis bytes andsize_tieris not provided, or ifimageis a pixel array andsize_tieris 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_tieris negative or does not fit in an unsigned 64-bit integer, since the key appends it as a little-endianu64.
- ValueError – If
-
Return type:
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!