I have an numpy array in python that represent an image its size is 28x28x3 while the max value of it is 0.2 and the min is -0.1. I want to scale that image between 0-255. How can I do so?
With the help of Numpy numpy. resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing.
In Python, numpy. size() function count the number of elements along a given axis. Parameters: arr: [array_like] Input data.
NumPy arrays have a fixed size at creation, unlike Python lists (which can grow dynamically). Changing the size of an ndarray will create a new array and delete the original. The elements in a NumPy array are all required to be of the same data type, and thus will be the same size in memory.
Resize() resize() function is used to create a new array of different sizes and dimensions. resize() can create an array of larger size than the original array.
new_arr = ((arr + 0.1) * (1/0.3) * 255).astype('uint8')
This first scales the vector to the [0, 1] range, multiplies it by 255 and then converts it to uint8
, which is a common format for images (opencv uses it, for example)
In general you can use:
new_arr = ((arr - arr.min()) * (1/(arr.max() - arr.min()) * 255)).astype('uint8')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With