# Perceptual Image Hash API

> Perceptual hashes of PNG / JPEG images (aHash, dHash, pHash, blockhash), Hamming distance between two images, and grouping of up to 200 near-duplicates.

- Endpoint: `GET | POST https://apibox.io.vn/v1/image-phash`
- Category: Images & Codes
- HTML docs: https://apibox.io.vn/docs/image-phash/ (Vietnamese: https://apibox.io.vn/vi/docs/image-phash/)
- Online tool: https://apibox.io.vn/tools/image-phash/
- Authentication: `Authorization: Bearer <API key>` or `X-API-Key: <API key>` (free key: https://apibox.io.vn/dashboard/#signup)

Perceptual hashes stay almost the same when a picture is resized, re-compressed, converted or lightly edited, so they find duplicate and near-duplicate images where a file checksum fails. `action=hash` (one image: `file`, `file_base64` or `url`) returns aHash, dHash and pHash (64 bits, 16 hex digits, the definitions of the Python imagehash library) and blockhash (256 bits, blockhash.io); `known` (a list of hashes you keep) returns the ones within `threshold`.

`action=compare` takes two images (`a` + `b`, `file` + `file2` or `url` + `url2`) or two hashes (`hash1` + `hash2`) and returns the Hamming distance, a similarity score and a verdict per algorithm (`identical_hash`, `near_duplicate`, `similar`, `different`; default thresholds 10 / 64 bits and 40 / 256 bits). `action=group` takes `images` (up to 200: URLs, base64 strings or `{id, url | base64 | phash}` objects) and returns groups of near-duplicates with the number of unique images.

pHash uses a 32×32 discrete cosine transform of the image reduced by area averaging. Crops, rotations and mirror images are not matched. PNG and JPEG are decoded (up to 12.5 megapixels); convert WebP / HEIC first. This is not face recognition, and images and hashes are not stored.

## Parameters

| Name | In | Type | Required | Default | Description |
|---|---|---|---|---|---|
| `action` | body|query | string | no | `hash` | `hash`, `compare` or `group` (guessed from the inputs when omitted). |
| `file` | body | file | no |  | Image (PNG / JPEG), or `file_base64`, or `url`. |
| `a` | body | file | no |  | compare: first image (`b` = second; or `file2`, `url2`). |
| `hash1` | body|query | string | no |  | compare: hex hash (with `hash2`). |
| `images` | body | array | no |  | group: up to 200 URLs, base64 strings or `{id, url \| base64 \| phash}` objects. |
| `algorithm` | body|query | string | no | `phash` | group / hash comparison: `ahash`, `dhash`, `phash` or `blockhash`. |
| `algorithms` | body|query | string | no | `all` | hash / compare: comma list of algorithms. |
| `threshold` | body|query | number | no |  | Maximum Hamming distance for a near duplicate (default 10, blockhash 40). |
| `known` | body | array | no |  | hash: your list of known hashes (`"hex"` or `{id, hash}`) to match against. |

## Example request

```bash
curl -X POST "https://apibox.io.vn/v1/image-phash" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -F "action=@./my-file"
```

## Example response

```json
{
  "image1": {
    "label": "photo.png",
    "width": 640,
    "height": 480
  },
  "image2": {
    "label": "photo-q40.jpg",
    "width": 640,
    "height": 480
  },
  "verdict": "identical_hash",
  "verdict_vi": "trùng hash (gần như chắc chắn cùng ảnh)",
  "decided_by": "phash",
  "results": {
    "ahash": {
      "hash1": "401b030373649818",
      "hash2": "401b030373649818",
      "distance": 0,
      "bits": 64,
      "similarity": 1,
      "verdict": "identical_hash",
      "verdict_vi": "trùng hash (gần như chắc chắn cùng ảnh)",
      "threshold": 10
    },
    "dhash": {
      "hash1": "a3b3362ec2cc3032",
      "hash2": "83b3b66ec2cc3032",
      "distance": 3,
      "bits": 64,
      "similarity": 0.9531,
      "verdict": "near_duplicate",
      "verdict_vi": "gần trùng",
      "threshold": 10
    },
    "phash": {
      "hash1": "8d2958c3657c73c9",
      "hash2": "8d2958c3657c73c9",
      "distance": 0,
      "bits": 64,
      "similarity": 1,
      "verdict": "identical_hash",
      "verdict_vi": "trùng hash (gần như chắc chắn cùng ảnh)",
      "threshold": 10
    },
    "blockhash": {
      "hash1": "30003f8767c767c701df001f001f07ff1f0e1f341c7c1e78cffcc7c483c403cc",
      "hash2": "3000378777c767c701df001f035f07ff1f0e1f341c7c1e78cff8c7c483c403ce",
      "distance": 7,
      "bits": 256,
      "similarity": 0.9727,
      "verdict": "near_duplicate",
      "verdict_vi": "gần trùng",
      "threshold": 40
    }
  },
  "note": "Perceptual hashes survive resizing, re-compression, format change and small edits, not crops, rotations or mirroring. Distances are Hamming distances; thresholds are heuristics. Images are not stored."
}
```

## Use cases

- Detect re-posted product photos on a marketplace or classifieds site
- Clean duplicate pictures from a CMS or digital asset library
- Check contest entries against previously submitted photos
- Match uploads against your own list of known image hashes

## FAQ

### Which algorithm should I use?

pHash is the most robust for re-compressed and resized photos; dHash is fast and good for exact re-uploads; blockhash (256 bits) separates similar-looking images better. compare returns all of them.

### What distance means "the same image"?

For 64-bit hashes, 0–10 is usually the same picture (resized, re-compressed, small watermark); above about 18 the images are different. Tune threshold for your data.

### Does it find cropped or rotated copies?

No. Perceptual hashes compare the whole frame; crops, rotations and mirroring change the hash. Use feature matching for those cases.
