I'm trying to add a scalar value at certain indices in a numpy array, which is of type numpy.uint8
. Is there a way for me to make sure the addition clips at a certain maximum, so that the sum doesn't overflow? Here's how achieve it with a for loop, but it is slow and inefficient. Is there a way to use np.add.at
or another function to do this faster?
change_by = 80
indices = [(0,0), (100, 100), (23, 45)]
for idx in indices:
output_image[idx[0], idx[1]] = min(255, image[idx[0], idx[1]] + change_by)
This limits the max value for each element to be 255
. Is there a more efficient way to achieve this? Thanks!