As your comment indicates, this is not int s; they are numpy.uint8 s. Just convert them to int s:
>>> a, b = map(numpy.uint8, (50, 60)) >>> a - b __main__:1: RuntimeWarning: overflow encountered in ubyte_scalars 246 >>> a, b = map(int, (a, b)) >>> a - b -10
Since you're worried about speed, here are a couple of tests (borrowing Sven with thanks):
>>> %timeit abs(int(a) - int(b)) 1000000 loops, best of 3: 410 ns per loop >>> %timeit a - b if a > b else b - a 1000000 loops, best of 3: 470 ns per loop
So yes, itโs faster, but if we are not talking about doing it hundreds of millions of times, it doesnโt matter.
source share