Python 正在尝试显示RGB值的列表

Python 正在尝试显示RGB值的列表,python,matplotlib,python-imaging-library,rgb,Python,Matplotlib,Python Imaging Library,Rgb,我有一个列表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,

我有一个列表
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
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
5#向图像中添加条形图
6 barFullData=[x代表条形图中的x]*barImg.size[1]
---->7 barImg.putdata(barFullData)
8.
9#用于存储条形图的文件夹
putdata中的C:\Users\meezy\Anaconda3\lib\site packages\PIL\Image.py(self、data、scale、offset)
1456自抄本()
1457
->1458自im.putdata(数据、比例、偏移)
1459
1460 def putpalette(自身、数据、rawmode=“RGB”):
TypeError:应为整数参数,得到浮点值

您可以使用
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)
请注意,如果原始颜色值是浮动的,则需要将其缩小到范围
[0,1]

输出:


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

Pil图像应包含整数。我建议如下使用PIL和numpy:

import numpy as np
from PIL import Image

barColors = [(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 = (np.array(barColors)).astype(np.uint8)

title = "p"
#creating bar image
cols = len(barColors)
rows = max([1,int(cols/2.5)])

# Create color Array
barFullData = np.tile(barColors, (rows,1)).reshape(rows, cols, 3)
# Create Image from Array
barImg = Image.fromarray(barFullData, 'RGB')

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

放大版:


除了给出的答案之外,另一个选择是使用seaborn的
帕尔普洛特

导入seaborn作为sns
colors=np.array([
(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)]) / 255.0
sns.帕尔普洛特(彩色)

我在将绘图窗口拖动到适当的宽度和高度后得到了图像
Perplot
有一个控制高度的
size
参数。拯救 使用
fig=plt.gcf()
然后使用
fig.savefig
或直接 使用
plt.savefig()


希望这有帮助。

我想你的意思是
plt.savefig
<代码>gca返回一个
。谢谢!我想先使用
fig=plt.gcf()
,然后再使用
fig.savefig()
,但是直接使用
plt.savefig()
可能更容易。有没有一种Pythonic的方法将我必须的元组列表转换成一个带有元组的numpy数组,而不是重新键入所有值?np.array(barColors)不起作用,假设
barColors
有元组?