Read set of images into 4D Numpy array with dimension (num_img,channel, dim1, dim2)
<p>I have a set of 1000 gray scale images (28x28), I want to read them into 4D numpy array (number of images, 1, img_dim1,img_dim2). Following is my code but it doesn't work properly. Any idea how I can fix the issue in the code? </p>

<pre><code>from PIL import Image
import numpy as np
import os

mypath=os.path.dirname('path/to/directory/')
def load_dataset( ) :
    data =np.zeros((1000,1,28,28), dtype=np.float64)
    for fname in os.listdir(mypath):
        pathname = os.path.join(mypath, fname)
        img = Image.open(pathname)
        data = np.dstack((data, img))
    return data
data=load_dataset()
print(data.shape)
</code></pre>