Skip to content

Commit

Permalink
Merge pull request JohannesBuchner#54 from mynameisvinn/master
Browse files Browse the repository at this point in the history
disallow negative hash size arguments
  • Loading branch information
JohannesBuchner authored May 8, 2017
2 parents 6e840a9 + d060233 commit 29c34ed
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ build
dist
ImageHash.egg-info/
.eggs
.DS_Store
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ A image hashing library written in Python. ImageHash supports:

|Travis|_ |Coveralls|_

Rationale
---------
Why can’t we use md5, sha-1, etc.?

Unfortunately, we cannot use cryptographic hashing algorithms in our implementation. Due to the nature of cryptographic hashing algorithms, very tiny changes in the input file will result in a substantially different hash. In the case of image fingerprinting, we actually want our similar inputs to have similar output hashes as well.

Requirements
-------------
Based on PIL/Pillow Image, numpy and scipy.fftpack (for pHash)
Expand Down
8 changes: 8 additions & 0 deletions imagehash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ def average_hash(image, hash_size=8):
@image must be a PIL instance.
"""
if hash_size < 0:
raise ValueError("Hash size must be positive")

# reduce size and complexity, then covert to grayscale
image = image.convert("L").resize((hash_size, hash_size), Image.ANTIALIAS)
Expand All @@ -143,6 +145,9 @@ def phash(image, hash_size=8, highfreq_factor=4):
@image must be a PIL instance.
"""
if hash_size < 0:
raise ValueError("Hash size must be positive")

import scipy.fftpack
img_size = hash_size * highfreq_factor
image = image.convert("L").resize((img_size, img_size), Image.ANTIALIAS)
Expand Down Expand Up @@ -184,6 +189,9 @@ def dhash(image, hash_size=8):
@image must be a PIL instance.
"""
# resize(w, h), but numpy.array((h, w))
if hash_size < 0:
raise ValueError("Hash size must be positive")

image = image.convert("L").resize((hash_size + 1, hash_size), Image.ANTIALIAS)
pixels = numpy.array(image.getdata(), dtype=numpy.float).reshape((hash_size, hash_size + 1))
# compute differences between columns
Expand Down
4 changes: 4 additions & 0 deletions imagehash/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ def check_hash_stored(self, func, image):
'- stringified hash {}'.format(distance, image_hash,
other_hash))
self.assertEqual(distance, 0, emsg)

def check_hash_size(self, func, image, size):
with self.assertRaises(ValueError):
func(image, -1)
4 changes: 4 additions & 0 deletions imagehash/tests/test_average_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def test_average_hash_length(self):
def test_average_hash_stored(self):
self.check_hash_stored(self.func, self.image)

def test_average_hash_size(self):
self.check_hash_size(self.func, self.image, -1)



if __name__ == '__main__':
unittest.main()
2 changes: 2 additions & 0 deletions imagehash/tests/test_dhash.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def test_dhash_length(self):
def test_dhash_stored(self):
self.check_hash_stored(self.func, self.image)

def test_dhash_size(self):
self.check_hash_size(self.func, self.image, -1)

if __name__ == '__main__':
unittest.main()
4 changes: 3 additions & 1 deletion imagehash/tests/test_phash.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def test_phash_length(self):

def test_phash_stored(self):
self.check_hash_stored(self.func, self.image)


def test_phash_size(self):
self.check_hash_size(self.func, self.image, -1)

if __name__ == '__main__':
unittest.main()
1 change: 0 additions & 1 deletion imagehash/tests/test_whash.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,5 @@ def test_image_scale_not_2power(self):
with six.assertRaisesRegex(self, AssertionError, emsg):
imagehash.whash(self.image, image_scale=image_scale+1)


if __name__ == '__main__':
unittest.main()

0 comments on commit 29c34ed

Please sign in to comment.