You can use a mask to use vectorization of numpy (implicit loops), which will be much faster:
mask = pixel_array == paddingVal byte_array[mask] = 0 byte_array[~mask] = np.round(255.0 * (pixel_array[~mask] - minVal) / (maxVal - minVal - 1.0))
It can also be done in a way that is cleaner because you do not need to create a byte_array in advance:
byte_array = np.round(255.0 * (pixel_array - minVal) / (maxVal - minVal - 1.0)).astype(np.uint8) byte_array[pixel_array == paddingVal] = 0
Edit: as Joe Kington points out in a comment on the question, this is memory trading for speed.
source share