开发者社区 问答 正文

如何将目录中的图像转换为NP数组

我将图像存储在目录中:

from pathlib import Path from PIL import Image import os, shutil from os import listdir

Image Resizing

from PIL import Image

load and display an image with Matplotlib

from matplotlib import image from matplotlib import pyplot

Image folder

images_dir = Path('C:\Users\HP\Desktop\XXXXXXXXXXXXXXXXX\images').expanduser() images_dir

dim = (400, 300)

Resizing all the images to same dimension

X_image_train = [] for fname in listdir(images_dir): fpath = os.path.join(images_dir, fname) im = Image.open(fpath) im_resized = im.resize(dim) X_image_train.append(im_resized)

Converting the image to numpy array

X_image_array=[] for x in range(len(X_image_train)): X_image=np.array(X_image_train[x],dtype='uint8') X_image_array.append(X_image)

Checking the size of a single image

X_image_array[0].shape

(300, 400, 3)

# Checking the size of a single image X_image_array[15].shape > (300, 400, 3)

我想将'X_image_array'转换为具有以下暗淡的张量:

(2000,300, 400, 3)

其中2000是“图像”文件夹中的样本数。

在这里需要帮助吗?

展开
收起
天枢2020 2020-05-11 14:07:23 1947 分享 版权
1 条回答
写回答
取消 提交回答
  • 代码改变世界,我们改变代码

    np.stack(X_image_array)

    2020-05-11 14:08:19
    赞同 展开评论
问答分类:
问答地址: