0

I have a float array like this [0.1,0.4,1.5,2.22,3] where max value is always 3.0

I want to Quantize that to a byte array from 0 to 255 for each value I want to Quantize it like (byte)((value / 3.0) * 255.0)

Is there a way to do that in numpy very fast rather than iterating every value in Python and rebuilding a new byte array?

1
  • You don't want to use 255.0, you want to use 255.9999. Otherwise your count of 255 will be severely underrepresented. Commented Jan 24, 2022 at 5:40

1 Answer 1

2

Use astype

import numpy as np

value = np.array([0.1, 0.4, 1.5, 2.22, 3])
out = ((value / 3.0) * 255.0).astype(np.ubyte)
print(out)

# Output
array([  8,  34, 127, 188, 255], dtype=uint8)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, works great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.