Reputation: 3275
What I'm doing is reducing colors in an image by quantization, but instead of using floats I need to translate into RGB (eg. array(255, 255, 255)). I've found similar questions but not a straightforward/direct solution.
Returning clustered
produces the array of floats. How do you convert the float to the RGB equivalent?
# Pixel Matrix
pixel = reshape(img,(img.shape[0]*img.shape[1],3))
# Clustering
centroids,_ = kmeans(pixel,8) # six colors will be found
# Quantization
qnt,_ = vq(pixel,centroids)
# Shape Quantization Result
centers_idx = reshape(qnt,(img.shape[0],img.shape[1]))
clustered = centroids[centers_idx]
Upvotes: 0
Views: 1317
Reputation: 27615
If you want to convert any array of floats to array of bytes (8-bit unsigned integers, from 0 to 255), you have some options. The one I prefer for more general conversions is this:
bytearray = (floatarray*255).astype('uint8')
This should work if you have any array of positive floats whose pixel values for each channel vary between 0.0 and 1.0. If you have arbitrary positive values, you could do floatarray /= floatarray.max()
first, to normalize the values.
Hope this helps!
Upvotes: 1