Question

I have a folder with many images (ordered by their creation time) that I can read into numpy float32 arrays. I want to write these arrays down to the filesystem in a single file in two different formats that a C programm (I can not modify) will access.

The first format is easy: The values for the arrays one after another from left to right from top to bottom for every array. (The arrays come one after another that way). That I can do with np.tofile trivially.

The second format is more complicated: For every pixel-coordinate (x, y) I want to write the corresponding pixels of all images one after another sequentially into the file. I tried to stack the arrays and then trnaspose the result. But when I write that down to the filesystem using np.tofile, the file contains the same arrangement of data as with the first format.

How can I tell numpy to rearrange the data?

Was it helpful?

Solution

For the second format, you could use column_stack followed by ravel

In [8]: img1 = np.arange(5, dtype='float32')

In [9]: img2 = np.arange(5, dtype='float32')

In [10]: np.column_stack((img1,img2)).ravel()
Out[10]: array([ 0.,  0.,  1.,  1.,  2.,  2.,  3.,  3.,  4.,  4.], dtype=float32)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top