gpt4 book ai didi

python - 在 reshape 图像阵列时感到困惑

转载 作者:太空宇宙 更新时间:2023-11-03 15:58:26 24 4
gpt4 key购买 nike

目前我正在尝试运行一个 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...

但我不知道如何 reshape 和连接 image 以获得与上面相同的结构。有人可以帮忙吗?

最佳答案

一种方法是转置轴,这实际上是在 fortran 模式下展平,即反转方式 -

image = np.asarray(im, dtype="uint8")
image_representation = image.ravel('F').tolist()

要更深入地了解该函数,请查看 numpy.ravel documentation .

关于python - 在 reshape 图像阵列时感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41863336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com