Add support for difference-aware hash calculation.

Fix indentation exception
This commit is contained in:
dust-to-dust 2022-04-08 10:51:57 +08:00 committed by GitHub
parent 998116bf08
commit 96e3c0a853
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2708,7 +2708,7 @@ class Image:
decimal_value = 0
return hash_string
def is_similar_to(self, image, hamming_distance: int = 3) -> bool:
def is_similar_to(self, image, hamming_distance: int = 3) -> bool:
"""
:param image: PIL.Image / dHash(str)
@ -2719,7 +2719,7 @@ def is_similar_to(self, image, hamming_distance: int = 3) -> bool:
dis = self.hamming_distance(image)
return True if dis <= hamming_distance else False
def hamming_distance(self, image) -> int:
def hamming_distance(self, image) -> int:
"""
Calculate the Hamming distance between two pictures (based on dHash algorithm)
:param image: PIL.Image / dHash(str)
@ -2734,8 +2734,8 @@ def hamming_distance(self, image) -> int:
return sum(image1_diff ^ image2_diff)
@staticmethod
def _difference(image):
@staticmethod
def _difference(image):
"""
*Private method*
Calculate difference sequence
@ -2756,7 +2756,7 @@ def _difference(image):
return array(difference)
def _hamming_distance_with_hash(self, dhash: str) -> int:
def _hamming_distance_with_hash(self, dhash: str) -> int:
"""
*Private method*
calculate hamming distance with perceptual hash