python-重塑图像数组时感到困惑
作者:互联网
目前,我正在尝试运行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...
但是我不知道如何重塑和连接图像以获得与上述相同的结构.有人可以帮忙吗?
解决方法:
一种方法是转置轴,这实际上是在fortran模式下变平的,即反向方式-
image = np.asarray(im, dtype="uint8")
image_representation = image.ravel('F').tolist()
要进一步了解该功能,请查看numpy.ravel documentation.
标签:conv-neural-network,python,numpy 来源: https://codeday.me/bug/20191111/2022380.html