gpt4 book ai didi

python - 将数组 reshape 为 rgb 矩阵

转载 作者:行者123 更新时间:2023-11-28 18:29:15 27 4
gpt4 key购买 nike

我已经在 numpy 数组中加载了 100x100 rgb 图像。然后我将其转换为 30000x1 numpy 数组以通过机器学习模型。该模型的输出也是一个 30000x1 的 numpy 数组。如何将此数组转换回 100x100 三元组的 numpy 数组,以便打印生成的 rgb 图像?

如果初始数组是[r1 g1 b1],[r2 g2 b2],...,[],它被展开为[r1 g1 b1 r2 g2 b2 .. .]。我需要它以 [r1 g1 b1],[r2 g2 b2],...,[] 的形式返回。

我用来将图像加载为数组的内容:

im=img.resize((height,width), Image.ANTIALIAS);
im=np.array(im);
im=im.ravel();

我试过 .reshape((100,100,3)) 并且得到了黑色输出图像。机器学习模型正确,不是黑屏的原因。

最佳答案

试试 reshape((3, 100, 100))

a = np.random.random((3, 2, 2))
# array([[[ 0.28623689, 0.96406455],
# [ 0.55002183, 0.73325715]],
#
# [[ 0.44293834, 0.08118479],
# [ 0.28732176, 0.94749812]],
#
# [[ 0.40169829, 0.0265604 ],
# [ 0.07904701, 0.19342463]]])
x = np.ravel()
# array([ 0.28623689, 0.96406455, 0.55002183, 0.73325715, 0.44293834,
# 0.08118479, 0.28732176, 0.94749812, 0.40169829, 0.0265604 ,
# 0.07904701, 0.19342463])
print(x.reshape((2, 2, 3)))
# array([[[ 0.28623689, 0.96406455, 0.55002183],
# [ 0.73325715, 0.44293834, 0.08118479]],

# [[ 0.28732176, 0.94749812, 0.40169829],
# [ 0.0265604 , 0.07904701, 0.19342463]]])
print(x.reshape((3, 2, 2)))
# array([[[ 0.28623689, 0.96406455],
# [ 0.55002183, 0.73325715]],
#
# [[ 0.44293834, 0.08118479],
# [ 0.28732176, 0.94749812]],
#
# [[ 0.40169829, 0.0265604 ],
# [ 0.07904701, 0.19342463]]])

关于python - 将数组 reshape 为 rgb 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38774977/

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