_binary_array_to_hex gives wrong value
See original GitHub issueCorrect me if I am wrong, but _binary_array_to_hex
gives wrong values.
Python 2.7.13 (default, Feb 16 2017, 19:11:00)
[GCC 5.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>> import imagehash
>>> img = Image.open('sample_image.png')
>>> ph = imagehash.phash(img)
>>> bool_array = ph.hash.flatten()
>>> bool_array
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], dtype=bool)
>>> bit_array = 1*bool_array
>>> bit_array
array([1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0])
>>> bit_string = ''.join(str(b) for b in bit_array)
>>> bit_string
'1001111000111100110000011111000011110000110000111110011111000000'
>>> int(bit_string, 2)
11402201597170935744L
>>> hex(int(bit_string, 2))
'0x9e3cc1f0f0c3e7c0L' # This is the expected hex value for the given bit array (phash)
>>> str(ph)
'793c830f0fc3e703' # and here is the actual value.
If I made no mistake, let me know and I will submit a PR.
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
Byte array to Hex string conversion in javascript - Stack Overflow
When converting a byte array to a hex array, we have to consider how they can be signed numbers. If so, we gotta...
Read more >Converting Between Byte Arrays and Hexadecimal Strings in ...
In this tutorial, we'll take a look at different ways to convert a byte array to a hexadecimal String, and vice versa.
Read more >Hex to ASCII conversion shows wrong result - Edureka
I am using the following code to convert Hex to ASCII: public String hexToAscii(String hex) { StringBuilder sb = new StringBuilder(); ...
Read more >2 Examples to Convert Byte[] Array to String in Java
Converting a byte array to String seems easy but what is difficult is, doing it correctly. Many programmers make the mistake of ignoring...
Read more >Hex (Apache Commons Codec 1.12 API)
Converts a String or an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’d like to reopen this issue, because the implementation of the binary-to-hexadecimal conversion is not interoperable with other implementations of the same algorithm, and simply not interoperable with “standard” value-conversion functions.
Therefore, if I calculate the pHash with
imagehash
and then I take its hex value and interpret it with another “standard” hex-to-bin function, I obtain a completely different value.When I say “standard” I mean something like “widely used”. Take for example the
bitstring.BitArray
class:versus
Again, for Hamming-distance comparison, as long as the bits are the same, it doesn’t matter. However, if you use other libraries, you might get weird results.
@phretor, could you check if the newest version (4.0) works OK for you?