Skip to main content

Perceptual hashes for images

Project description

Phim

A Python library to detect perceptual image duplicates.

About perceptual hashes

Perceptual hashing algorithms are efficient at detecting near duplicate images which arise, e.g. due to different resolutions or compression rates. These algorithms work by computing a hash for each image. This hash works as a sort of fingerprint, and similar images should have similar hashes. In particular, the hashes are binary vectors, and we count the number of differing entries (or bits) to measure image similarity.

Consider the following simplified example.

There are two images: img1 and img2 with these hashes:

      img1: 10010111
      img2: 10110100

We can represent those bit-vectors using two bytes: 151 for img1 and 180 for img2. In reality, the bit-vectors are longer, so we get additional bytes, which are stored as an array of 8-bit unsigned integers. However, they differ at three places

      img1: 10010111
      img2: 10110100
difference: --x---xx

so their Hamming distance is three.

Perceptual hashing algorithms

Phim supports two hashing algorithmns: pHash and PDQ-hash. They are both based on the discrete cosine transform (DCT) and have similar properties. The main difference is their resolution and which parts of the DCT they use. The PDQ-hash is a 256-bit perceptual hash algorithm made by Meta aiming for exchanging threat information between companies. The pHash algorithm is very similar, but it's a bit older and has only 64 bits.

Another difference between the PDQ-hash and the pHash is that the PDQ-hash computes the hash for all eight possible 90 degree rotation/mirror combinations by smartly transforming the DCT. The same trick could, in theory, be applied during the pHash calculations as well, but it is in general not supported by most implementations (including this one).

Phim has direct bindings for the pHash (via the imagehash Rust crate), and it provides a unified interface to the PDQ-hash as well. However, for the PDQ-hash you also need to install the pdqhash Python library, which binds the reference C++ implementation of the PDQ-hash made by the Facebook Threat Exchange.

Example

Simple pHash example

import phim
import PIL.Image as Image

img1 = Image.open(...)
img2 = Image.open(...)

phash1 = phim.compute_phash(img1)
phash2 = phim.compute_phash(img2)

differing_bits = phim.compute_hamming_distance(phash1, phash2)
print(f"There are {differing_bits} differing bits between the two pHashes")

Simple PDQ-hash example

import phim
import PIL.Image as Image

img1 = Image.open(...)
img2 = Image.open(...)

pdq_hash_combos1, quality1 = phim.compute_pdq_hash(img1)
pdq_hash_combos2, quality2 = phim.compute_pdq_hash(img2)

differing_bits = phim.compute_hamming_distances(pdq_hash_combos1[0], pdq_hash_combos2)
print(f"There are {min(differing_bits)} differing bits between the two PDQ-hashes")

More intricate example

import phim
import PIL.Image as Image

image_paths = ...
images = [Image.open(p) for p in image_paths]
query_img = Image.open(...)

phash = phim.compute_phash(query_img)
phashes = [phim.compute_phash(img) for img in images]

differing_bits = phim.compute_hamming_distances(phash, phashes)
most_similar_index = differing_bits.argmin()

print(f"The most similar image is {image_paths[most_similar_index]}")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

phim-0.0.4-cp311-abi3-win_amd64.whl (205.4 kB view details)

Uploaded CPython 3.11+Windows x86-64

phim-0.0.4-cp311-abi3-musllinux_1_2_x86_64.whl (563.2 kB view details)

Uploaded CPython 3.11+musllinux: musl 1.2+ x86-64

phim-0.0.4-cp311-abi3-musllinux_1_2_i686.whl (599.1 kB view details)

Uploaded CPython 3.11+musllinux: musl 1.2+ i686

phim-0.0.4-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (406.9 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ x86-64

phim-0.0.4-cp311-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (377.2 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ s390x

phim-0.0.4-cp311-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (804.1 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ppc64le

phim-0.0.4-cp311-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (356.8 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ARMv7l

phim-0.0.4-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (347.3 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ARM64

phim-0.0.4-cp311-abi3-manylinux_2_5_i686.manylinux1_i686.whl (383.6 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.5+ i686

phim-0.0.4-cp311-abi3-macosx_10_12_x86_64.whl (321.6 kB view details)

Uploaded CPython 3.11+macOS 10.12+ x86-64

phim-0.0.4-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (606.8 kB view details)

Uploaded CPython 3.11+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file phim-0.0.4-cp311-abi3-win_amd64.whl.

File metadata

  • Download URL: phim-0.0.4-cp311-abi3-win_amd64.whl
  • Upload date:
  • Size: 205.4 kB
  • Tags: CPython 3.11+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for phim-0.0.4-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 329df312cbb9a2cf952ca4e9d438f230173078daa241781cba85bf34692ddbd9
MD5 587b623f3b1faebad96169d72753d91d
BLAKE2b-256 bd48100cb393142778d0e256faf6c91e169db07326ec4401cb405bd828b76310

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.4-cp311-abi3-win_amd64.whl:

Publisher: release.yml on MarieRoald/phim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file phim-0.0.4-cp311-abi3-musllinux_1_2_x86_64.whl.

File metadata

  • Download URL: phim-0.0.4-cp311-abi3-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 563.2 kB
  • Tags: CPython 3.11+, musllinux: musl 1.2+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for phim-0.0.4-cp311-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 6088d505a4be5298b63db252fbc3c6a83a6e639fffee8bd8aabd698043a35a4e
MD5 c1d7eccab98c6d575fb35070344aa92b
BLAKE2b-256 d6b0ed1beb97480a5be35ba518249a17d3769183d25257b4a37400b7030eb152

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.4-cp311-abi3-musllinux_1_2_x86_64.whl:

Publisher: release.yml on MarieRoald/phim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file phim-0.0.4-cp311-abi3-musllinux_1_2_i686.whl.

File metadata

  • Download URL: phim-0.0.4-cp311-abi3-musllinux_1_2_i686.whl
  • Upload date:
  • Size: 599.1 kB
  • Tags: CPython 3.11+, musllinux: musl 1.2+ i686
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for phim-0.0.4-cp311-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 e0d23a31d7ec0da89aa0bba489c2b00fdf31a9914df90d1bba49a9e668ca80b2
MD5 9ed2552ec3a1b58b42c47a734572b3ac
BLAKE2b-256 7b7a1a8e12453b6457724a0dfec1067043d94da4f04d8ce6abf06b50433def96

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.4-cp311-abi3-musllinux_1_2_i686.whl:

Publisher: release.yml on MarieRoald/phim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file phim-0.0.4-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for phim-0.0.4-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9ad53aaf6da41861618a041b27f9dba6b5d2c383e4a76417bcb3049a24794282
MD5 e5f842c8c35c7f1ea01373ce25fc4298
BLAKE2b-256 9b313f920c134a802f75f13e8d46bbe224a7dae4df8a10b3382cbf185c92839b

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.4-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on MarieRoald/phim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file phim-0.0.4-cp311-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for phim-0.0.4-cp311-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 eebd1e63cd43d418882433d1b9a17b9e545c6844b5377bb46f68785c57b51d10
MD5 93a4793a636fa04da97682dc96090e72
BLAKE2b-256 cc8d4a344473b1a1060e3f63529105d631bb6999598291873fefdcaaf2f2a4d4

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.4-cp311-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl:

Publisher: release.yml on MarieRoald/phim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file phim-0.0.4-cp311-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for phim-0.0.4-cp311-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 267702550d7598e41a2eb114aee4696b7028c19a0d9f0675cb61af4615bdbb1c
MD5 662199b2be9344582d094aee73be6c9a
BLAKE2b-256 2c9b876ede9003a14b08d9a80d3c9de0bed53e76933888a7a0d8e3c9f5055f4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.4-cp311-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl:

Publisher: release.yml on MarieRoald/phim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file phim-0.0.4-cp311-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for phim-0.0.4-cp311-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 14638bc715222383576be59082ea0ca44d79376e6344529119c834f6689595d5
MD5 294181b23decf7f470d15463f9120d70
BLAKE2b-256 6b4e4d32e56bf26ac57d369d98d8b9f5697356226a687785c618b07c98a9ed4f

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.4-cp311-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl:

Publisher: release.yml on MarieRoald/phim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file phim-0.0.4-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for phim-0.0.4-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6005b22b7d2dc3dec5125769732a0eccc176a802f44c3dd21b1d21f7d3c38539
MD5 61af34f65f54b2170555e72c53bc1ca4
BLAKE2b-256 a97d958fbdee8099187dc76aeb4aa3d6de7f08575ad59e338832ce764189826f

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.4-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on MarieRoald/phim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file phim-0.0.4-cp311-abi3-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for phim-0.0.4-cp311-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 81de36799b5bd635f0b5eda02e31dc3a92dcca8ce13dd2f3b30f2bb3aa460bd0
MD5 dc7e6509bf487042bb6d7496a4269291
BLAKE2b-256 8e135caed11950ff82fa5e287e387a58dfa7af7ae5dbc111f99de3354b6a73a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.4-cp311-abi3-manylinux_2_5_i686.manylinux1_i686.whl:

Publisher: release.yml on MarieRoald/phim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file phim-0.0.4-cp311-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for phim-0.0.4-cp311-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 09a00af02de1a233c0640cdb841daa74b519b134578660b764a6bf3074dcdf2a
MD5 81fe0770900aa31a38d850c54757a67f
BLAKE2b-256 e63a45c82a7d2a497ce08887d061fc0f799bf28eda3b8ff55b32fb4827058393

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.4-cp311-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on MarieRoald/phim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file phim-0.0.4-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for phim-0.0.4-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 54743f7c48280fbec2f036f8eae9dd20ffeae98bfa8a624e83fdfbe2b338dfcf
MD5 0ca09c115dbddfb008646e02e4496ebb
BLAKE2b-256 663bd2a85d94a9e11b4b45a263cf8f4086fbefa90e3bd3886a2063d9c1d981f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.4-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl:

Publisher: release.yml on MarieRoald/phim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page