I'm casting an array that is populated with signed 32-bit integers to an array with only uint8 values (from 0 to 255). Right now, my code looks like this:
newArray = Image.fromarray(oldArray.astype(numpy.uint8))
The thing is: I want the numbers lower than zero to become zero, and the numbers higher than 255 to become 255, and this code obviously doesn't operate like that.
Is there an easy way to do it without iterating through the whole array?
For in-place thresholding, I would use numpy.clip(myArray,0,255) before casting your array into the new data type.
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