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_hashes(img1.convert("RGB"))
pdq_hash_combos2, quality2 = phim.compute_pdq_hashes(img2.convert("RGB"))

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
import numpy as np

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

phash = phim.compute_phash(query_img)
phashes = np.stack([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.6-cp311-abi3-win_amd64.whl (149.2 kB view details)

Uploaded CPython 3.11+Windows x86-64

phim-0.0.6-cp311-abi3-musllinux_1_2_x86_64.whl (496.2 kB view details)

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

phim-0.0.6-cp311-abi3-musllinux_1_2_i686.whl (529.5 kB view details)

Uploaded CPython 3.11+musllinux: musl 1.2+ i686

phim-0.0.6-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (295.5 kB view details)

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

phim-0.0.6-cp311-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (309.9 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ s390x

phim-0.0.6-cp311-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (404.8 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ppc64le

phim-0.0.6-cp311-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (295.5 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ARMv7l

phim-0.0.6-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (288.1 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ARM64

phim-0.0.6-cp311-abi3-manylinux_2_5_i686.manylinux1_i686.whl (311.8 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.5+ i686

phim-0.0.6-cp311-abi3-macosx_10_12_x86_64.whl (258.1 kB view details)

Uploaded CPython 3.11+macOS 10.12+ x86-64

phim-0.0.6-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (503.3 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.6-cp311-abi3-win_amd64.whl.

File metadata

  • Download URL: phim-0.0.6-cp311-abi3-win_amd64.whl
  • Upload date:
  • Size: 149.2 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.6-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 6586b6a39e9f698279f6a1b02f5ceef41909e814dc43f4fb56241d8c011993cb
MD5 f71a56bc23f445ba08c55c508f6e1a8a
BLAKE2b-256 edf12eb513f0533a5bfbeaa0c45369a6389f883e5dbafa81ffb7bc93d36e6b4e

See more details on using hashes here.

Provenance

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

Publisher: release.yml on NationalLibraryOfNorway/webdata-wp3-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.6-cp311-abi3-musllinux_1_2_x86_64.whl.

File metadata

  • Download URL: phim-0.0.6-cp311-abi3-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 496.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.6-cp311-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 063b22df162543e00c3234e997b2fb893ead77166821f003ba712d6f085dd3c9
MD5 fc3958852c1a16a5f01993ecbcdee936
BLAKE2b-256 399f71f0af387b79ca0a5945184fd5f61047411fca958abdb18926b1fbe03725

See more details on using hashes here.

Provenance

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

Publisher: release.yml on NationalLibraryOfNorway/webdata-wp3-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.6-cp311-abi3-musllinux_1_2_i686.whl.

File metadata

  • Download URL: phim-0.0.6-cp311-abi3-musllinux_1_2_i686.whl
  • Upload date:
  • Size: 529.5 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.6-cp311-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 bc9ea6d40b279b20757782a2bbffdb2e703dcecada9d46d76e6ba92ec5939b40
MD5 0f2d51af70a0ca1787605f9dc452203c
BLAKE2b-256 9dec4184d2531a308a7a1f3a49892361d630d4d98e529c69eb65bbb80c22961a

See more details on using hashes here.

Provenance

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

Publisher: release.yml on NationalLibraryOfNorway/webdata-wp3-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.6-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for phim-0.0.6-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 69bfef6c48fcf395380ee3f3c96ef7fecfc508daf6929f9d15b73bee04db4644
MD5 6b303d05ba8a95762844408ddf37c1dc
BLAKE2b-256 d07876f15a7099c46dad97a0deb687f3a6e341ecfe22011e73b2d7238325698f

See more details on using hashes here.

Provenance

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

Publisher: release.yml on NationalLibraryOfNorway/webdata-wp3-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.6-cp311-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for phim-0.0.6-cp311-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 93f31e1f1c081b6a9802f061d903c6e411985b4879d8e01815dc35f2cdec168e
MD5 ad0327e8bb0cf3e1d87aa319884dba93
BLAKE2b-256 ea122c2c5feb5ad75c153c8617ad3c846ff720101d68b4217320913e98d44a08

See more details on using hashes here.

Provenance

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

Publisher: release.yml on NationalLibraryOfNorway/webdata-wp3-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.6-cp311-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for phim-0.0.6-cp311-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 fd7d65594283a850ddae81d6eea1b9e15b36de4e15c921a118d1d90080c28d33
MD5 3c9919ffd498f88856a80a24079ea8e2
BLAKE2b-256 c08844483458d2cd4cae69a7f42110b1fb33c4fa27da8e06b2da763e4f56f793

See more details on using hashes here.

Provenance

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

Publisher: release.yml on NationalLibraryOfNorway/webdata-wp3-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.6-cp311-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for phim-0.0.6-cp311-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 115b7787e96fa26dc5ac948e0420308a82e517928078d62bcf3f4a7ceba8182d
MD5 a140357252f55cfec0cc9fd1a3f76624
BLAKE2b-256 bcac9ec82e7be489e777471ab2f819107c448ef58e92b7d364f77ff95ff826d6

See more details on using hashes here.

Provenance

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

Publisher: release.yml on NationalLibraryOfNorway/webdata-wp3-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.6-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for phim-0.0.6-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 50dce79f85783d92f34df0d2a0fbbabd5ddd3ea99554f19ba20c7ed9364af3c2
MD5 b86408d31c738d594e99603729d89cc7
BLAKE2b-256 65d611f6786bbada03bda88abe009cdf80805b15ebaf9613b04a956dc4cb55b2

See more details on using hashes here.

Provenance

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

Publisher: release.yml on NationalLibraryOfNorway/webdata-wp3-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.6-cp311-abi3-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for phim-0.0.6-cp311-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 96b3500173cbc6445e71491653662bf9a8c28e2064488b9301dda96970a94c30
MD5 456749b07b891ebce6109f1bbf79eef1
BLAKE2b-256 379c2a5f40d489f778e66fa9bb139789729aec87956ecac6c6ac165cda7e128f

See more details on using hashes here.

Provenance

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

Publisher: release.yml on NationalLibraryOfNorway/webdata-wp3-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.6-cp311-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for phim-0.0.6-cp311-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d374d03bf676025d4d5ba6b64f34b1713d7b3d71b05eb372f7d90303adbdb82a
MD5 1c9e6c13a97afe43056c3cdcca1e40d8
BLAKE2b-256 9d52c011ac112d30a2e68edc265b51e670a9331f184b40a6615dbb08bd63f19e

See more details on using hashes here.

Provenance

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

Publisher: release.yml on NationalLibraryOfNorway/webdata-wp3-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.6-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.6-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 401619631b6df86a26510303c2281d75a869c49bb5754f7faa658862a817d6b2
MD5 f794c9e519fd74ee25c84a7629b5971a
BLAKE2b-256 50547cf496b5f725c0a733e6d68f8f613d5fef2abf9c241b1f4d29a990becfad

See more details on using hashes here.

Provenance

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

Publisher: release.yml on NationalLibraryOfNorway/webdata-wp3-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