I'm using the following function with Python2.7:
def array2int(pixels):
out = 0
for bit in pixels:
out = (out << 1) | bit
return out
Which usually works, but if I pass
v=np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
array2int(v.astype(int))
it returns -262145.
out = (out << 1) | int(bit)is a quick fixint("".join(map(str, v)), 2)int("".join(map(lambda x: str(int(x)), v.astype(bool))), 2)