# Hashing

## hash

```python
hash(source: Source, algorithm: Uint64Algorithm | Uint64Name = ..., *, config: Config | None = ...) -> int
```

```python
hash(source: Source, algorithm: DigestAlgorithm | DigestName, *, config: Config | None = ...) -> Digest
```

```python
hash(source: Source, algorithm: AlgorithmLike, *, config: Config | None = ...) -> int | Digest
```

```python
hash(source: Source, algorithm: AlgorithmLike = PHASH, *, config: Config | None = None) -> int | Digest
```

Hash one image.

`source` is a path, encoded bytes, a binary file object, an array of pixels or a Pillow image. Returns an `int` for aHash, dHash, pHash and wHash, a :class:`Digest` for the others.

## ImageContext

```python
ImageContext(config: Config | None = None)
```

One image and the configuration to hash it with: a libphash `ph_context_t`.

Load an image with one of the `load_*` methods, then compute any number of hashes from it; loading the next image reuses the context's buffers, which makes a context kept across a loop faster than :func:`hash` per image.

A context is safe to share between threads: the calls into C are serialized on a lock of the context. Threads that work in parallel each need their own context (or a batch, :func:`hash_files`). Close it with :meth:`close` or a `with` block; one left to the garbage collector is freed too.

### width

```python
width: int
```

Width of the loaded image (after orientation and decode scale), 0 if none.

### channels

```python
channels: int
```

3 for color, 1 for grayscale.

An alpha channel is resolved at load and not kept.

### close

```python
close() -> None
```

Free the C context. Idempotent; any later use raises ValueError.

### load

```python
load(source: Source) -> None
```

Load whatever `source` is.

`source` is a path, encoded bytes, a binary file object, an array of pixels or a Pillow image. Bytes are always the encoded file, never a path.

### load\_file

```python
load_file(path: StrPath) -> None
```

Load an image file. On failure the context holds no image.

### load\_bytes

```python
load_bytes(data: bytes | bytearray | memoryview) -> None
```

Load an encoded image (JPEG, PNG, WebP, ...) from memory, without a copy.

The buffer must not change while the call runs. On failure the context holds no image.

### load\_fileobj

```python
load_fileobj(fileobj: SupportsRead) -> None
```

Read a binary file object to its end and load the bytes.

The object is an open file, an `io.BytesIO`, a response body or anything else with a `read()` that returns bytes.

### load\_pixels

```python
load_pixels(pixels: PixelArray, channel_order: ChannelOrder = 'rgb') -> None
```

Load decoded pixels.

`pixels` is an 8-bit array of shape (h, w), (h, w, 1), (h, w, 3) or (h, w, 4), e.g. a numpy `uint8` array. `channel_order="bgr"` reads OpenCV's order. The pixels are copied into the context, and no EXIF orientation applies. On failure the previously loaded image stays loaded.

### load\_pil

```python
load_pil(image: PILImage) -> None
```

Load a Pillow image.

:func:`pixels_from_pil` describes how each mode is read.

### hash

```python
hash(algorithm: Uint64Algorithm | Uint64Name = ...) -> int
```

```python
hash(algorithm: DigestAlgorithm | DigestName) -> Digest
```

```python
hash(algorithm: AlgorithmLike) -> int | Digest
```

```python
hash(algorithm: AlgorithmLike = PHASH) -> int | Digest
```

Compute one hash of the loaded image.

Returns an `int` for aHash, dHash, pHash and wHash, a :class:`Digest` for the others.

### hashes

```python
hashes(*algorithms: AlgorithmLike) -> dict[Algorithm, int | Digest]
```

Compute several hashes at once, all of the same loaded image.

The 64-bit ones share their work on the full image (one `ph_compute_multi` call); digests are computed one by one. No other thread loads an image in between.

### ahash

```python
ahash() -> int
```

Average hash: each cell of an 8x8 grayscale reduction against their mean.

### dhash

```python
dhash() -> int
```

Difference hash: horizontal neighbor comparisons in a 9x8 reduction.

### phash

```python
phash() -> int
```

Perceptual hash: the low-frequency DCT block of a reduction.

Each coefficient is compared with the block's median.

### whash

```python
whash() -> int
```

Wavelet hash: the low-frequency Haar band of a reduction.

Each coefficient is compared with the band's median.

### bmh

```python
bmh() -> Digest
```

Block mean hash: one bit per block of a `bmh_block_size` grid (BITS).

### mhash

```python
mhash() -> Digest
```

Marr-Hildreth hash: a Laplacian-of-Gaussian response (BITS).

Nine bits per window, 576 bits in all.

### radial

```python
radial() -> Digest
```

Radial variance hash: rotation-tolerant projections (COEFFICIENTS).

### color\_hash

```python
color_hash() -> Digest
```

Color histogram, compared by histogram intersection (HISTOGRAM).

### color\_moments

```python
color_moments() -> Digest
```

Color moments per channel (VECTOR16).

The mean, the standard deviation and the cube root of the third moment.

## Algorithm

Bases: `IntEnum`

A hash algorithm; the values are libphash's `ph_algorithm_t`.

Wherever an algorithm is expected, its libphash name (`"phash"`, `"color_moments"`) is accepted too; `str(algorithm)` is that name. How each one works and what it survives is on libphash's site, one page per algorithm, and [Choosing an algorithm](<https://gudoshnikovn.github.io/libphash/theory/choosing/>) sets them side by side.

### AHASH

```python
AHASH = 0
```

[aHash](<https://gudoshnikovn.github.io/libphash/theory/ahash/>), the average hash: each cell of an 8-by-8 thumbnail against their mean. A 64-bit `int`.

### DHASH

```python
DHASH = 1
```

[dHash](<https://gudoshnikovn.github.io/libphash/theory/dhash/>), the difference hash: each cell of a 9-by-8 thumbnail against its neighbor. A 64-bit `int`.

### PHASH

```python
PHASH = 2
```

[pHash](<https://gudoshnikovn.github.io/libphash/theory/phash/>), the perceptual hash and the default: the coarsest cosine patterns of the DCT. A 64-bit `int`.

### WHASH

```python
WHASH = 3
```

[wHash](<https://gudoshnikovn.github.io/libphash/theory/whash/>), the wavelet hash: the Haar wavelet's average against its median. A 64-bit `int`.

### BMH

```python
BMH = 4
```

[BMH](<https://gudoshnikovn.github.io/libphash/theory/bmh/>), the block mean hash: a grid of block means against their median. A `Digest` of bits.

### MHASH

```python
MHASH = 5
```

[mHash](<https://gudoshnikovn.github.io/libphash/theory/mhash/>), the Marr-Hildreth hash: edges and detail through the Mexican-hat filter. A `Digest` of bits.

### RADIAL

```python
RADIAL = 6
```

[Radial](<https://gudoshnikovn.github.io/libphash/theory/radial/>), the radial variance hash: the variance along lines through the center, which survives rotation. A `Digest` of coefficients.

### COLOR\_HASH

```python
COLOR_HASH = 7
```

[ColorHash](<https://gudoshnikovn.github.io/libphash/theory/color-hash/>), a histogram of which colors the picture is made of. A `Digest` histogram.

### COLOR\_MOMENTS

```python
COLOR_MOMENTS = 8
```

[ColorMoments](<https://gudoshnikovn.github.io/libphash/theory/color-moments/>), the mean, spread and skew of each color channel. A `Digest` of 16-bit features.
