我想从目录加载相同的图像,并使用python重塑形状重塑它们.
我怎样才能做到这一点?
假设您安装了scipy并假设使用"reshape"实际意味着"调整大小",以下代码应该加载目录中的所有图像/foo/bar
,将它们调整为64x64并将它们添加到列表中images
:
import os from scipy import ndimage, misc images = [] for root, dirnames, filenames in os.walk("/foo/bar"): for filename in filenames: if re.search("\.(jpg|jpeg|png|bmp|tiff)$", filename): filepath = os.path.join(root, filename) image = ndimage.imread(filepath, mode="RGB") image_resized = misc.imresize(image, (64, 64)) images.append(image_resized)
如果你需要一个numpy数组(要调用reshape
),那么只需images = np.array(images)
在末尾添加(import numpy as np
在开始时).