我正在尝试将带有图像的文件夹解析为numpy数组。我想获得一个看起来像这样的数组:
import numpy as np
from sklearn.datasets import fetch_mldata
#load 70,000 MNIST images (28 X 28 pixels)
mnist = fetch_mldata("MNIST original")
X = mnist.data
print X.shape
期望的输出:
(70000L, 784L)
这就是我的尝试:
from PIL import Image
import numpy
import matplotlib.pyplot as plt
import glob
#I have two colour images, each 64 X 64 pixels, in the folder
imageFolderPath = 'C:\\Users\\apples\\Desktop\\t-sne\\pics'
imagePath = glob.glob(imageFolderPath+'/*.JPEG')
im_array = numpy.array( [numpy.array(Image.open(imagePath[i])) for i in range(len(imagePath))] )
print im_array.shape
但它会产生以下输出:
(2L, 64L, 64L, 3L)
如何获得具有以下尺寸的阵列:
(m, n)
答案 0 :(得分:2)
PIL以RGB格式(可能并非总是)加载彩色图像,因此最后一个尺寸为3(每个颜色通道一个)。因此,您可能希望将图像转换为其他pixel format。您还需要展平图像阵列以获得所需的布局。你可以这样做:
def img2array(path):
img = Image.open(path).convert('F')
return numpy.array(img).ravel()
im_array = numpy.array([img2array(path) for path in imagePath])