I have a BGRA image dumped to a binary file in the following format (serially): [(b, g, r, a), (b, g, r, a), (b, g, r, a) ...] I know the image width, height & # of channels (4 of course in this case).
I want to read the image into a 4D array where the first dimension represents the B values, etc. I have the following code:
data = np.fromfile(fbin, np.dtype('B'))
print data
This prints something like:
[ 79 90 92 0 80 91 93 0 84 96 98 0 ...]
Where 79 is B, 90 is G, 92 is R and 0 is A, and so on. Now I tried to reshape 'data' like this:
print data.reshape(channels, height, width)
Got the following:
[[[ 79 90 92 0 ...] .. [] ..]
[[109 ...] .. [] ..]
[[118 ...] .. [] ..]
[[ 0 ...] .. [] ..]]
Where as what I would like to get is something like this:
[[[ 79 ...] .. [] ..]
[[90 ...] .. [] ..]
[[92...] .. [] ..]
[[0...] .. [] ..]]
Well, this feels almost too easy, the solution is:
data = data.reshape(channels, width, height, order='F')
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