I have two functions that behave the same way with int64 but work differently with uint8:
A_int64 = np.array([1, 2, 3])
B_unit8 = A.astype(np.uint8)
def scale_image(image):
minv = np.amin(image)
maxv = np.amax(image)
result = (255 * (image - minv) / (maxv - minv))
return result.astype(np.uint8)
def scale_image_multiple_lines(image):
minv = np.amin(image)
maxv = np.amax(image)
image = image - minv
image = image / (maxv - minv)
image = image * 255
return image.astype(np.uint8)
print(scale_image(A_int64)) # [ 0 127 255]
print(scale_image_multiple_lines(A_int64)) # [ 0 127 255]
print(scale_image(B_unit8)) # [ 0 127 127]
print(scale_image_multiple_lines(B_unit8)) # [ 0 127 255]
The one that doesn't work as intended is print(scale_image(B_unit8)) # [ 0 127 127]
My guess is that the multiline version works with both datatypes because it casts earlier. However, since I don't believe I'm dealing with negatives (I subtract the minimum value but not more than that, I don't think) why should it make a difference?