<分区>
我想从目录中加载相同的图像并使用 python 使用 reshape 函数 reshape 它们。
我该怎么做?
<分区>
我想从目录中加载相同的图像并使用 python 使用 reshape 函数 reshape 它们。
我该怎么做?
最佳答案
假设您有 scipy安装并假设“reshape”实际上是指“resize”,下面的代码应该加载目录 /foo/bar
中的所有图像,将它们调整为 64x64 并将它们添加到列表 图片
:
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
开头)。
关于python - 使用 python 从目录加载图像并 reshape ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34426949/