我正在编写一个函数来从图像中读取像素数据,并将它们存储在numpy数组中,以便进一步进行训练/测试拆分。
当我运行这段代码时,它抛出了一个异常,说明除了连接轴之外,所有输入数组的维数都必须完全匹配。
我不确定为什么会发生这个问题,以及如何修复它。
from PIL import Image
import numpy as np
import os
X = np.array([])
y = []
categories = {
'A': 1,
'B': 2
}
root = data_dir + '/cropped_resized(128,128)/'
for path, subdirs, files in os.walk(root):
for name in files:
img_path = os.path.join(path,name)
category = categories[os.path.basename(path)]
im = Image.open(img_path)
img_pixels = list(im.getdata())
width, height = im.size
X = np.vstack((X, img_pixels))
#X = np.concatenate((X, img_pixels), axis=0)
y.append(category)
X_train, X_test, y_train, y_test = train_test_split(X, y)下面是一个失败的图片示例

发布于 2019-04-03 19:35:05
决定你想要你的图像是RGB还是Greyscale,并确保它们是加载的。
具体地说,更改此行:
im = Image.open(img_path)至
im = Image.open(img_path).convert('RGB')或
im = Image.open(img_path).convert('L')https://stackoverflow.com/questions/55493231
复制相似问题