How do I reshape array of images obtained from .h5 file in python?
<p>I'm new to python and I apologize in advance if my question seems trivial.</p>

<p>I have a .h5 file containing pairs of images in greyscale organized in match and non-match groups. For my final purpose I need to consider each pair of images as a single image of 2 channels (where each channel is in fact an image). </p>

<p>To use my data I proceed like this:</p>

<ol>
<li><p>I read the .h5 file putting my data in numpy arrays (I read both groups match and non-match, both with shape (50000,4096)):</p>

<pre><code> with h5py.File('supervised_64x64.h5','r') as hf:
     match = hf.get('/match')
     non_match = hf.get('/non-match')
     np_m = np.asarray(match)
     np_nm = np.asarray(non_match)
     hf.close()
</code></pre></li>
<li><p>Then I try to reshape the arrays:</p>

<pre><code>reshaped_data_m = np_m.reshape(250000,2,4096)
</code></pre></li>
</ol>

<p>Now, if I reshape the arrays as (250000,2,4096) and then I try to show the corresponding images what I get is actually right. But I need to reshape the arrays as (25000,64,64,2) and when I try to do this I get all black images. </p>

<p>Can you help me? 
Thank you in advance!</p>

<p>I bet you need to first transpose your input matrix from <code>250000x2x4096</code> to <code>250000x4096x2</code>, after which you can do the <code>reshape</code>.</p>

<p>Luckly, numpy offers the <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.transpose.html" rel="nofollow">transpose</a> function which should do the trick. See <a href="http://stackoverflow.com/questions/23943379/swapping-the-dimensions-of-a-numpy-array">this question</a> for a bigger discussion around transposing.</p>

<p>In your particular case, the invocation would be:</p>

<pre><code>transposed_data_m = numpy.transpose(np_m, [1, 3, 2])
reshaped_data_m = tranposed_data_m.reshape(250000, 64, 64, 2)
</code></pre>

python