forked from bunchesofdonald/photohash
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a fix for the given problems: 1. When a hash is generated with hash_size=6 _binary_array_to_hex will give a 8 digit hexadecimal representation for it while the correct would be a 9 digit hecadecimal representation. Retrieving a hash from the hex value in these cases is impossible. 2. _binary_array_to_hex gives no output when hash_size=2. 3. The _binary-to-hexadecimal conversion is not interoperable with other implementations of the same algorithm. Fixing _binary_array_to_hex demands hex_to_hash to be changed as well. That makes hex_to_hash incompatible with hex values generate with previous versions of ImageHash. For that reason, the old implementation is available as old_hex_to_hash in order to allow users convert old hex values back to hashes. Regarding the new version of hex_to_hash: - It assumes all hashes are bidimensional arrays with dimensions hash_size * hash_size. This is the current behavior of all hashes. - The implementation does not work for hash_size < 2.
- Loading branch information
Showing
7 changed files
with
166 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import unittest | ||
import numpy as np | ||
import imagehash | ||
|
||
|
||
# Each row is a test case where the first value is a bit sequence and | ||
# the second value is the expected hexadecimal representation for it. | ||
binary_to_hexadecimal_values = [ | ||
['1', '1'], | ||
['11', '3'], | ||
['111', '7'], | ||
['1111', 'f'], | ||
['10000', '10'], | ||
['110000', '30'], | ||
['1110000', '70'], | ||
['11110000', 'f0'], | ||
['00001', '01'], | ||
['000011', '03'], | ||
['0000111', '07'], | ||
['00001111', '0f'], | ||
['10000001', '81'], | ||
['00000000000000001', '00001'], | ||
['000000000000000011', '00003'], | ||
['0000000000000000111', '00007'], | ||
['00000000000000001111', '0000f'], | ||
['11110000111100001111', 'f0f0f'], | ||
['00001111000011110000', '0f0f0'], | ||
['11110000000100100011010001010110011110001001101010111100110111101111', 'f0123456789abcdef'], | ||
['1001111000111100110000011111000011110000110000111110011111000000', '9e3cc1f0f0c3e7c0'], | ||
['1000111100001111000011110000111100001111000010110000101101111010', '8f0f0f0f0f0b0b7a'], | ||
] | ||
|
||
# Each row is a test case where the first value is a hexadecimal sequence | ||
# and the second value is the expected binary representation for it. | ||
hexadecimal_to_binary_values = [ | ||
['1', '0001'], | ||
['2', '0010'], | ||
['3', '0011'], | ||
['a', '1010'], | ||
['f', '1111'], | ||
['101', '100000001'], | ||
['1b1', '110110001'], | ||
['0b1', '010110001'], | ||
['f0f0', '1111000011110000'], | ||
['0f0f', '0000111100001111'], | ||
['000c', '0000000000001100'], | ||
['100000d', '1000000000000000000001101'], | ||
['000000d', '0000000000000000000001101'], | ||
['000000001', '000000000000000000000000000000000001'], | ||
['800000001', '100000000000000000000000000000000001'], | ||
['0000000000001', '0000000000000000000000000000000000000000000000001'], | ||
['1000000000001', '1000000000000000000000000000000000000000000000001'], | ||
['0000000000000001', '0000000000000000000000000000000000000000000000000000000000000001'], | ||
['8000000000000001', '1000000000000000000000000000000000000000000000000000000000000001'], | ||
] | ||
|
||
# Each row is a test case where the first value is a hexadecimal | ||
# sequence and the second value is the expected bool array for it. | ||
hexadecimal_to_bool_array = [ | ||
['9e3cc1f0f0c3e7c0', np.array([ [True, False, False, True, True, True, True, False], | ||
[False, False, True, True, True, True, False, False], | ||
[True, True, False, False, False, False, False, True], | ||
[True, True, True, True, False, False, False, False], | ||
[True, True, True, True, False, False, False, False], | ||
[True, True, False, False, False, False, True, True], | ||
[True, True, True, False, False, True, True, True], | ||
[True, True, False, False, False, False, False, False]]) ], | ||
] | ||
|
||
class TestHexConversions(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.to_hex = imagehash._binary_array_to_hex | ||
self.from_hex = imagehash.hex_to_hash | ||
|
||
def test_binary_array_to_hex_input(self): | ||
for case in hexadecimal_to_bool_array: | ||
self.assertEqual(case[0], self.to_hex(case[1])) | ||
|
||
def test_hex_to_hash_output(self): | ||
for case in hexadecimal_to_bool_array: | ||
self.assertTrue(np.array_equal(case[1], self.from_hex(case[0]).hash)) | ||
|
||
def test_conversion_to_hex(self): | ||
for case in binary_to_hexadecimal_values: | ||
expected = case[1] | ||
bit_array = np.array([int(d) for d in case[0]]) | ||
result = self.to_hex(bit_array) | ||
self.assertEqual(expected, result) | ||
|
||
def test_conversion_from_hex(self): | ||
for case in hexadecimal_to_binary_values: | ||
expected = case[1] | ||
result = ''.join(str(b) for b in 1 * self.from_hex(case[0]).hash.flatten()) | ||
self.assertEqual(expected, result) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import unittest | ||
import numpy as np | ||
import imagehash | ||
|
||
|
||
# Each row is a test case where the first value is a hexadecimal | ||
# sequence and the second value is the expected bool array for it. | ||
old_hexadecimal_to_bool_array = [ | ||
['ffeb89818193ffff', np.array([ [True, True, True, True, True, True, True, True], | ||
[True, True, False, True, False, True, True, True], | ||
[True, False, False, True, False, False, False, True], | ||
[True, False, False, False, False, False, False, True], | ||
[True, False, False, False, False, False, False, True], | ||
[True, True, False, False, True, False, False, True], | ||
[True, True, True, True, True, True, True, True], | ||
[True, True, True, True, True, True, True, True]]) ], | ||
] | ||
|
||
class TestOldHexConversions(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.from_hex = imagehash.old_hex_to_hash | ||
|
||
def test_hex_to_hash_output(self): | ||
for case in old_hexadecimal_to_bool_array: | ||
self.assertTrue(np.array_equal(case[1], self.from_hex(case[0]).hash)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters