I want to convert a numpy
matrix with positive and negative values of type np.float
to a grayscale image with pixels from [0,255]
. I want to do that as fast as possible.
Right now I am doing the following:
import numpy as np
n = 1000
I = np.random.randn(n,n)
I_min = np.min(I)
I = I + np.abs(I_min)
I_max = np.max(I)
I = 255 * (I / I_max)
I = I.astype(np.uint8)
I hope that is correct!? Is there a faster way to do that? What can be improved?