Convert list of PNGImageFile to array of array
I have a dataset organized in this way: /dataset/train/class/images.png (the same for test) (and I have 2 classes, positive and negative).
I want to obtain x_train, y_train, x_test and y_test, so I am using this python script:
x_train = []
y_train = []
x_test = []
y_test = []
base_dir_train = 'Montgomery_real_splitted/TRAIN/'
base_dir_test = 'Montgomery_real_splitted/TEST/'
for f in sorted(os.listdir(base_dir_train)):
    if os.path.isdir(base_dir_train+f):
        print(f&quot;{f} is a target class&quot;)
        for i in sorted(os.listdir(base_dir_train+f)):
            y_train.append(f)
            im = Image.open(base_dir_train+f+'/'+i)
            x_train.append(im)
for f in sorted(os.listdir(base_dir_test)):
    if os.path.isdir(base_dir_test+f):
        print(f&quot;{f} is a target class&quot;)
        for i in sorted(os.listdir(base_dir_test+f)):
            y_test.append(f)
            imt=Image.open(base_dir_test+f+'/'+i)
            x_test.append(imt)
            
      
y_train = np.array(y_train)
y_test = np.array(y_test)

Basically I obtain what I want, for example x_train is this:
[&lt;PIL.PngImagePlugin.PngImageFile image mode=L size=4892x4020 at 0x10A98B280&gt;,
 &lt;PIL.PngImagePlugin.PngImageFile image mode=L size=4020x4892 at 0x10A98B040&gt;,
...
 &lt;PIL.PngImagePlugin.PngImageFile image mode=L size=4020x4892 at 0x11BA5D940&gt;,
 &lt;PIL.PngImagePlugin.PngImageFile image mode=L size=4020x4892 at 0x11BA5D9A0&gt;]

And y_train is:
array(['neg', 'neg', 'neg', 'neg', 'neg', 'neg', 'neg', 'neg', 'neg',
        ...
       'pos', 'pos', 'pos', 'pos', 'pos', 'pos', 'pos', 'pos', 'pos'],
      dtype='&lt;U3')


However, I want that x_train is in this format:
array([[[0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        ...,
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0]],

        ...

        ...,
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0]]], dtype=uint8)

How can I convert it?
EDIT for @Pyaive Oleg If I do im = np.array(im) then the result is this, which is different from the one that I want. The same for the tensor
[array([[ 0,  0,  0, ...,  0,  0,  0],
        [ 0,  0,  0, ...,  0,  0,  0],
        [ 0,  0,  0, ...,  0,  0,  0],
        ...,
        [ 5,  6,  7, ..., 14,  9,  5],
        [ 4,  5,  6, ..., 12,  8,  4],
        [ 0,  1,  2, ...,  3,  2,  0]], dtype=uint8),
 array([[  1,   1,   1, ...,   8, 246,   0],
        [  1,   1,   1, ...,   0,   7,  11],
        [  1,   1,   1, ...,   0,   0,   6],
        ...,
        [  0,   0,   0, ...,   0,   0,   0],
        [  0,   0,   0, ...,   0,   0,   0],
        [  0,   0,   0, ...,   0,   0,   0]], dtype=uint8),
        ...
        [  0,   0,   0, ...,   0,   0,   0],
        [  0,   0,   0, ...,   0,   0,   0],
        [  0,   0,   0, ...,   0,   0,   0]], dtype=uint8),
 array([[  0,   0,   0, ...,   0, 255,   1],
        [  0,   0,   0, ...,   0,   3,  11],
        [  0,   0,   0, ...,   2,   0,   7],
        ...,
        [  0,   0,   0, ...,   0,   0,   0],
        [  0,   0,   0, ...,   0,   0,   0],
        [  0,   0,   0, ...,   0,   0,   0]], dtype=uint8),
 array([[  1,   1,   1, ...,  19, 246,   0],
        [  1,   1,   1, ...,   0,  16,   0],
        [  1,   1,   1, ...,   2,   0,  12],
        ...,
        [  0,   0,   0, ...,   0,   0,   0],
        [  0,   0,   0, ...,   0,   0,   0],
        [  0,   0,   0, ...,   0,   0,   0]], dtype=uint8),
 


Before appending imt to x_train, do this:
imt = np.array(imt)

The following also can help:
from torchvision import transforms
imt = transforms.ToTensor()(imt)

