gpt4 book ai didi

python - 尝试显示 RGB 值列表

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

我有一个 barColors 列表,其中包含我要绘制的 RGB 值:

[(120.0304, 117.9008, 122.6944),
(66.3952, 65.0592, 69.088),
(22.2944, 24.3504, 26.5872),
(22.5744, 24.8352, 26.9152),
(0.0, 0.0, 0.0),
(94.864, 81.6416, 67.2272),
(92.5328, 79.6288, 66.2928),
(102.104, 86.7856, 71.3408),
(77.664, 65.0288, 52.712),
(78.2688, 69.3488, 60.1936),
(19.0432, 17.696, 17.7792),
(20.0432, 22.4064, 27.5456),
(30.7776, 32.4288, 36.5024),
(46.192, 49.8928, 54.7008),
(45.8016, 48.328, 55.1968),
.
.
.

我想为列表 barColors 中的每种颜色(行)创建一个带有垂直切片的图像。

我试过:

title = "p"
#creating bar image
barImg = Image.new("RGB",(len(barColors), max([1,int(len(barColors)/2.5)])))

#adding bars to the image
barFullData = [x for x in barColors] * barImg.size[1]
barImg.putdata(barFullData)

#folder to store bar images
if not os.path.isdir("bars"):
os.mkdir("bars")


#saving image
barImg.save("bars/{}_{}.png".format(title,method))
barImg.show()

但出现错误:

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-42-5a3d9f5e3a90> in <module>()
5 #adding bars to the image
6 barFullData = [x for x in barColors] * barImg.size[1]
----> 7 barImg.putdata(barFullData)
8
9 #folder to store bar images

C:\Users\meezy\Anaconda3\lib\site-packages\PIL\Image.py in putdata(self, data, scale, offset)
1456 self._copy()
1457
-> 1458 self.im.putdata(data, scale, offset)
1459
1460 def putpalette(self, data, rawmode="RGB"):

TypeError: integer argument expected, got float

最佳答案

您可以使用 axvspan 为每种颜色绘制一个条形图:

from matplotlib import pyplot as plt

scaled_colours = [[color / 255 for color in row] for row in colours]

fig, ax = plt.subplots(figsize=(6, 6))

ax.axis(xmin=0, xmax=len(scaled_colours))
ax.tick_params(left=False, labelleft=False, bottom=False, labelbottom=False)

for index, colour in enumerate(scaled_colours):
ax.axvspan(index, index + 1, color=colour)

请注意,如果您的原始颜色值为 float ,则需要将它们缩小到[0, 1] 范围内。

输出:

enter image description here

然后您可以使用 fig.savefig 保存结果。

关于python - 尝试显示 RGB 值列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56069551/

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