Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python make RGB image from 3 float32 numpy arrays

I have 3 arrays which are 400x600, which represent the 3 colors of the image i want to make.

I found a potential way here: http://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.misc.imsave.html but they want me to convert the floats to uint8's. Now if I change the dtype by 'image.dtype = np.uint8' then somehow changes the dimention to 400x600x24 (while when I don't change the type it is 400x600x3)

How do I change this? (Other methods also welcome)

like image 424
Coolcrab Avatar asked Nov 13 '14 21:11

Coolcrab


1 Answers

image.dtype = np.uint8 just forcibly casts the bytes from float64 to uint8. Since each float64 takes 8 bytes, and each uint8 is only 1 byte, you're getting 8 times as many values.

To convert the values, instead of reinterpreting the bytes, you want the astype method:

image = image.astype(np.uint8)

However, that probably isn't going to be very useful, for two reasons. First, the big one, your float values are probably all in the range 0.0-1.0. Second, astype truncates, rather than rounding. So, converting to integers is just going to make almost all of them 0, and the rest 1, rather than smoothly ranging from 0-255.

So, what you probably want is something like:

image = (image * 255).round().astype(np.uint8)
like image 116
abarnert Avatar answered Sep 29 '22 16:09

abarnert