r/Python • • Feb 17 '26

Discussion [ Removed by moderator ]

[removed] — view removed post

47 Upvotes

33 comments sorted by

View all comments

1

u/mass_coffee_dev Feb 17 '26

Union-Find is a really clean choice here. I did something similar for cleaning up a self-hosted Nextcloud instance and went with BK-trees for the nearest-neighbor lookup instead of bucketed prefixes. The nice thing about BK-trees is they give you exact Hamming distance queries without needing to tune bucket sizes, but your prefix bucketing is probably faster for the common case where most images aren't duplicates at all.

The dry-run + quarantine approach is the right call. I lost a bunch of wedding photos years ago from a dedup script that was a little too aggressive with pHash alone -- turned out some professionally edited versions had nearly identical hashes to the originals but were the ones I actually wanted to keep. Multi-hash corroboration would have caught that.

Curious about one thing: how do you handle HEIC vs JPEG versions of the same photo? iOS exports create that situation constantly and the compression artifacts are different enough that perceptual hashes can diverge more than you'd expect.

0

u/hdw_coder Feb 18 '26

Good point — HEIC↔JPEG is one of the trickier cases because the codec artifacts differ enough that perceptual hashes can drift more than expected (especially on foliage/texture).

In my current version they’re treated format-agnostically: load → EXIF transpose → thumbnail → multi-hash (dHash/pHash/wHash [+ colorhash]) with conservative thresholds + corroboration. Many HEIC/JPEG pairs still match, but some will miss if Hamming distances cross thresholds.

 The next improvement I’m considering is a ‘format-crossing tolerance band’. If one file is HEIC and the other is JPEG, allow a slightly higher dHash distance only if pHash+wHash corroborate strongly (and optionally run SSIM on borderline pairs). That boosts recall for iOS export duplicates without loosening the whole system and increasing false positives. Proposing concrete threshold numbers for a HEIC/JPEG pass (safe defaults) would be difficult as the values depend on hash_size, thumbnail size, and whether you’re using ImageOps.exif_transpose().