Matt M
Matt M

Reputation: 47

Trying to display list of RGB Values

I have a list barColors full of RGB values that I am trying to plot:

[(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),
.
.
.

I'd like to create an image with vertical slices for each color (row) in the list barColors.

I have tried:

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()

but am getting error:

---------------------------------------------------------------------------
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

Upvotes: 3

Views: 3176

Answers (3)

Prasanth
Prasanth

Reputation: 412

In addition to the answers given another option is to use seaborn's palplot.

import seaborn as 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.palplot(colors)

vertical bars of colors

I got the image after dragging the plot window to appropriate width and height. palplot has a size parameter that controls the height. Save the figure using fig = plt.gcf() and then fig.savefig or directly using plt.savefig()

Hope this helps.

Upvotes: 2

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339660

Pil images should contain integers. I would recommend using PIL and numpy as follows:

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()

enter image description here

Enlarged version:

enter image description here

Upvotes: 1

gmds
gmds

Reputation: 19895

You can use axvspan to plot one bar per colour:

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)

Note that if your original colour values are floats, they need to be scaled down to the range [0, 1].

Output:

enter image description here

You can then save the result with fig.savefig.

Upvotes: 2

Related Questions