question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

_binary_array_to_hex gives wrong value

See original GitHub issue

Correct 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:closed
  • Created 7 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
phretorcommented, Oct 26, 2017

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:

In [3]: BitArray(bin='1000111100001111000011110000111100001111000010110000101101111010')
Out[3]: BitArray('0x8f0f0f0f0f0b0b7a')

versus

In [13]: _binary_array_to_hex(np.array([bool(int(x)) for x in '1000111100001111000011110000111100001111000010110000101101111010']))
Out[13]: 'f1f0f0f0f0d0d05e'

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.

0reactions
JohannesBuchnercommented, Dec 7, 2017

@phretor, could you check if the newest version (4.0) works OK for you?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found