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 comes with an efficient pHash implementation and 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.5-cp311-abi3-win_amd64.whl (160.1 kB view details)

Uploaded CPython 3.11+Windows x86-64

phim-0.0.5-cp311-abi3-musllinux_1_2_x86_64.whl (511.2 kB view details)

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

phim-0.0.5-cp311-abi3-musllinux_1_2_i686.whl (542.7 kB view details)

Uploaded CPython 3.11+musllinux: musl 1.2+ i686

phim-0.0.5-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (311.7 kB view details)

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

phim-0.0.5-cp311-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (324.7 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ s390x

phim-0.0.5-cp311-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (425.7 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ppc64le

phim-0.0.5-cp311-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (310.5 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ARMv7l

phim-0.0.5-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (300.8 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ARM64

phim-0.0.5-cp311-abi3-manylinux_2_5_i686.manylinux1_i686.whl (325.6 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.5+ i686

phim-0.0.5-cp311-abi3-macosx_10_12_x86_64.whl (273.1 kB view details)

Uploaded CPython 3.11+macOS 10.12+ x86-64

phim-0.0.5-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (518.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.5-cp311-abi3-win_amd64.whl.

File metadata

  • Download URL: phim-0.0.5-cp311-abi3-win_amd64.whl
  • Upload date:
  • Size: 160.1 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.5-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 6b67208521ada76c7a9aef991ac153f796ac11c043c3527becfe5838a1547fff
MD5 648c26e145690a01cf01406446a93366
BLAKE2b-256 5aa33752c1e210429203ddbf4ad3cf6087794053f92213e5c551f1ac8c53296c

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.5-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.5-cp311-abi3-musllinux_1_2_x86_64.whl.

File metadata

  • Download URL: phim-0.0.5-cp311-abi3-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 511.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.5-cp311-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e1f4cd05b988af73b4a16eff175ce6fab44c835a215bbd6b3dc73987505f3705
MD5 3bd19fe24bb667e6ba5bd28d77d58aee
BLAKE2b-256 cfbf705d6cca2cfc11c2f19b92ce69717705e89d5198a19da152be82e3fa1ee2

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.5-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.5-cp311-abi3-musllinux_1_2_i686.whl.

File metadata

  • Download URL: phim-0.0.5-cp311-abi3-musllinux_1_2_i686.whl
  • Upload date:
  • Size: 542.7 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.5-cp311-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 11c1769ba9a21b1de77dc207ed425b46693a0510fafc41eff1336616fe85f0e7
MD5 b2299be3c0c483015e954ab0793629c9
BLAKE2b-256 948eee36cdd4488fc17d57cd075dc33a72162e71ac5e867e0876a737223fd947

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.5-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.5-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for phim-0.0.5-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d774425a7d7ebd37eab950d4abb05725d1a2f25475eb23734d21c49297240a4c
MD5 d6daeb76f636cde89a8fd52d032fb6db
BLAKE2b-256 94f61161402869e061b3f8f749476f7056cae80c7871c2b8c34d9a8553aa36d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.5-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.5-cp311-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for phim-0.0.5-cp311-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 c02c9c3091a85f315cd0c8778b76e2bc4f1081f5b6197f17f71f1e0995634ef7
MD5 1b05360a92ccf575beaeaf2877363005
BLAKE2b-256 df65237b60570b51310e4f0770c061948d90149cc90ee3ba345259854eef0a47

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.5-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.5-cp311-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for phim-0.0.5-cp311-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 4533cd114b61cf8432ffe64bffc6b3bd3d120ec7c961e3ecaf4efb8183925c98
MD5 0644888d5a9277008248be351b2b4e10
BLAKE2b-256 74234aaab1fe0106c911133716dcb822c1d88a0b838c59318188971ffd28676e

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.5-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.5-cp311-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for phim-0.0.5-cp311-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 38ec09b6abcee77f437e314b267f13915f55d787288aa92970d7fc3a7ca633ef
MD5 dd39d66f96712a079c763ff5d7299672
BLAKE2b-256 fdc99a28aa87adc1fba55c5ad3cd9339f245c4e436a749a2bb537719c8d8fd7f

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.5-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.5-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for phim-0.0.5-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 3bcd7c44199e06e967d6e9953ed963782e19ad93d37d4104c7eca79e97d41c5e
MD5 fd7ba105bac59c6006f1667af6a034fb
BLAKE2b-256 5561ff0832c864377cd03e5d56b0e8880b4bd57f884b817f2fbbf155879910b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.5-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.5-cp311-abi3-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for phim-0.0.5-cp311-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 3821841332e82008e1e57d6e8688dbe752522786fa2f0104d5b242c335e9019b
MD5 59f0d3033b7ce4cea46961c2c4332c6f
BLAKE2b-256 663d7e538b72580070c124d4ac2c20ffecac70026ccd7f3af683a258fda69e89

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.5-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.5-cp311-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for phim-0.0.5-cp311-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 717e4245510d001132fa601bbf1fd8bbe4cad764aedc580b17e3e9f1b1f16e98
MD5 4ded1235832f5d36281554c9fd9db366
BLAKE2b-256 fedeb6e35fa62d64b963d76bfe9b3707931e26d1b96b6f6e994ea65ccf49eaaf

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.5-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.5-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.5-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 3da9a27ff87fd71776fff13af012c899ebcc1daf128db287be50903a659b9f64
MD5 119a19e31a2c4a820a23c57c5477086c
BLAKE2b-256 31b05f878373fdd68f3e75f56919b7ccb66afa6fb0086c4d7344afed0ce7c80f

See more details on using hashes here.

Provenance

The following attestation bundles were made for phim-0.0.5-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