gpt4 book ai didi

python - 图像的 RGB 立方体

转载 作者:太空宇宙 更新时间:2023-11-04 06:14:36 25 4
gpt4 key购买 nike

我正在尝试在 python 中使用 matplotlib 创建图像的 RGB 立方体。我有一个 python 列表,其中所有像素的 RGB 格式为 [(2,152,255),(0,0,0)...]。我用散点函数绘制了所有点,但我不知道如何用各自的 RGB 颜色绘制每个 RGB 点。

我试过像这样 ax.scatter(paleta[0],paleta[1],paleta[2],c = RGBlist),但该函数需要一个 RGBa 值。 ..

我期待这样的结果:

enter image description here

代码:

paleta=zip(*RGBlist)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(paleta[0],paleta[1],paleta[2])
ax.grid(False)
ax.set_title('grid on')
plt.savefig('Images\\RGBcube.png')

最佳答案

尝试将 RGB 值缩放到 [0,1] 范围内:

ax.scatter(paleta[0],paleta[1],paleta[2], c=[(r[0]/255., r[1]/255., r[2]/255.) 对于 RGBlist 中的 r])

这适用于以下示例:

import random
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D

RGBlist = [(random.randint(0,255), random.randint(0,255), random.randint(0,255)) for i in range(100)]
paleta=zip(*RGBlist)
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(paleta[0],paleta[1],paleta[2], c=[(r[0] / 255., r[1] / 255., r[2] / 255.) for r in RGBlist])
ax.grid(False)
ax.set_title('grid on')
plt.savefig('blah.png')

给出输出:

enter image description here

关于python - 图像的 RGB 立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16551809/

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