我现在正在尝试运行一个ConvNet。每个图像,最终被馈送到神经网络中,都以列表形式存储。但是目前使用了三个for循环来创建该列表。看一下:
im = Image.open(os.path.join(p_input_directory, item))
pix = im.load()
image_representation = []
# Get image into byte array
for color in range(0, 3):
for x in range(0, 32):
for y in range(0, 32):
image_representation.append(pix[x, y][color])
我相信这不是最好的、最有效的方法。因为我必须遵循上面创建的列表的结构,所以我考虑使用numpy
提供一种到达相同结构的另一种方式。
from PIL import Image
import numpy as np
image = Image.open(os.path.join(p_input_directory, item))
image.load()
image = np.asarray(image, dtype="uint8")
image = np.reshape(image, 3072)
# Sth is missing here...
但我不知道如何重塑和连接 image
以获得与上述相同的结构。有人可以帮忙吗?