# Folder of images to a tensor

**URL:** <https://discuss.pytorch.org/t/folder-of-images-to-a-tensor/128879>\
**Category:** Uncategorized\
**Created:** [August 8, 2021, 11:40am UTC](https://discuss.pytorch.org/t/folder-of-images-to-a-tensor/128879 "2021-08-08T11:40:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gabriella\_m](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gabriella_m/32/41082_2.png) [@Gabriella\_m](https://discuss.pytorch.org/u/Gabriella_m)\
**Post date:** [August 8, 2021, 11:40am UTC](https://discuss.pytorch.org/t/folder-of-images-to-a-tensor/128879/1 "2021-08-08T11:40:34Z")

</div>

Hi, I made algorithm that loads images from a folder as numpy arrays or PIL images. Now I am not sure how to fit these images to a tensor of a shape

```auto
torch.Size([64, 1, 28, 28])

```

by the way they are MNIST images, I want to make my own loader

Right now i have my 64 images as numpy arrays forms. In array.

How to fit them into torch.Size([64, 1, 28, 28]) ?

EDIT: For clear info, i created normal python array and every image as numpy array form iam apending to this array.  
Now i want to make from this torch.Size([64, 1, 28, 28])

EDIT2: My images have shape - (28x28)

---

<div class="post-metadata">

**Author:** ![Vrushank98](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vrushank98/32/60269_2.png) [@Vrushank98](https://discuss.pytorch.org/u/Vrushank98)\
**Post date:** [August 8, 2021, 11:59am UTC](https://discuss.pytorch.org/t/folder-of-images-to-a-tensor/128879/2 "2021-08-08T11:59:16Z")

</div>

You can use,

> torchvision.transforms.ToTensor()

---

<div class="post-metadata">

**Author:** ![JosueCom](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/josuecom/32/27043_2.png) [@JosueCom](https://discuss.pytorch.org/u/JosueCom)\
**Post date:** [August 8, 2021, 5:44pm UTC](https://discuss.pytorch.org/t/folder-of-images-to-a-tensor/128879/3 "2021-08-08T17:44:30Z")

</div>

You can also convert each image before it goes to the array to a tensor via `imgs.append(torch.from_numpy(img))`, then use `torch.stack(imgs)` to turn the array into a tensor.
