numpydeep-learningpython-imaging-libraryreshapeself-supervised-learning

Numpy reshape image to column major order is duplicating my picture


For Self supervised learning purpose, I need to create a dataset composed of custom pictures. I want to store my pictures in a bin file, as numpy array in the shape (3, 256, 256) in column major order.

My picture is of shape (256, 256, 3) as tiff file. I open it using PIL.Image as usual.

However, using the code below I obtain the following result :

im = Image.open(file)
imarr = np.array(im)
imarr = imarr.reshape((3, 256, 256), order='F')

# for plotting
img = np.transpose(imarr, (1, 2, 0))
plt.imshow(img)
plt.title('generate bin files')
plt.show()

enter image description here

As specification, using no order or other values as 'C' or 'A' result in this kind of bugged pics: enter image description here

How can I make it so img is only one of the 9 replicates of my picture ? Thank you for your help.


Solution

  • Actually I found out that I misused np.reshape.

    Instead of

    imarr = imarr.reshape((3, 256, 256), order='F')
    

    I use

    imarr = np.transpose(imarr, (2, 0, 1))
    

    to have shape (3, 256, 256) and it works like a charm.