我使用numpy和matplotlib在python中导入图像。我想装箱一个三维数组,这样在第1和第2轴我有像素值,在第0轴我有图像编号。到目前为止我得到了这样的消息:from PIL import Image
import numpy
import matplotlib.pyplot as plt
import glob
imageFolderPath = '/home/B/Pictures/'
imagePath = glob.glob(imageFolderPath+'/*.JPG')
im_array = numpy.array(Image.open(imagePath[0]).convert('L'), 'f')
im_array = numpy.expand_dims(im_array, axis=0)
for c in range(1, len(imagePath)):
im_array_new = numpy.array(Image.open(imagePath[c]).convert('L'), 'f')
im_array_new = numpy.expand_dims(im_array_new, axis=0)
im_array = numpy.append(im_array, im_array_new, axis=0)
这工作,但有点难看。我不喜欢这样的事实,我必须扩展二维数组的维度,然后将它们附加在一起。
在python中有没有更优雅的方法来实现这一点?可能不需要预先分配一个巨大的3d数组(n个照片,x维,y维)