#hash #image #fuzzy #convert-images #pdq

pdqhash

Perceptual Hashing, originally designed by Facebook. Identify the same image even after some modifications.

2 releases

0.1.1 Jul 8, 2022
0.1.0 Jul 8, 2022

#58 in #convert-images

Download history 202/week @ 2024-03-14 158/week @ 2024-03-21 113/week @ 2024-03-28 97/week @ 2024-04-04 61/week @ 2024-04-11 109/week @ 2024-04-18 52/week @ 2024-04-25 95/week @ 2024-05-02 98/week @ 2024-05-09 97/week @ 2024-05-16 117/week @ 2024-05-23 109/week @ 2024-05-30 117/week @ 2024-06-06 118/week @ 2024-06-13 118/week @ 2024-06-20 81/week @ 2024-06-27

464 downloads per month

Apache-2.0

4MB
601 lines

PDQ Hash

This hash is a pure Rust implementation of the PDQ algorithm described here.

Calculation

The PDQ algorithm was developed and open-sourced by Facebook (now Meta) in 2019. It specifies a transformation which converts images into a binary format ('PDQ Hash') whereby 'perceptually similar’ images produce similar outputs. It was designed to offer an industry standard for representing images to collaborate on threat mitigation.

Obtain PDQ Hash of an image

Comparing two images reduces to computing distance (for example, Hamming distance) between their representations, or as % bit similarity. Comparing two bit strings

(16 bits are just used here for easier interpretation; PDQ hashes represent 256 bits)

Consider additional image transformations

Additionally, PDQ hashes for rotations and mirrors of the original image can be inferred efficiently, by just manipulating the Discrete Cosine Transform created in latter stages of processing. Comparing two bit strings Example: PDQ Hash of mirrored original image only requires manipulation of the transform

DCT Manipulation needed for corresponding PDQ Hash

  • Mirrored Y -> Negate alternate columns
  • Mirrored X -> Negate alternate rows
  • Mirrored Main Diagonal -> Transpose
  • Mirrored Off Diagonal -> Negate off-diagonal, transpose
  • Rotated 90 -> Negate alternate columns, transpose
  • Rotated 180 -> Negate off-diagonal
  • Rotated 270 -> Negate alternate rows, transpose

Offering similarity resilience

The resulting hashes are resilient to certain transformations, some more so than others, to detect additional attempted manipulation. Generally, images retaining overall structure are more resilient than changes to pixel positions and larger areas of pixel change. Obtain PDQ Hash of an image

Transformations that result in similar hashes:

  • File format change
  • Quality reduction
  • Light crops and shifts
  • Rotations (when additional hashes compared)
  • Resizing
  • Light watermarks
  • Mirroring (when additional hashes compared)
  • Noise or filter applied
  • Light logos

Dependencies

~13MB
~57K SLoC