问题
I have a list of RGB triplets, and I'd like to plot them in such a way that they form something like a spectrum.
I've converted them to HSV, which people seem to recommend.
from PIL import Image, ImageDraw
import colorsys
def make_rainbow_rgb(colors, width, height):
"""colors is an array of RGB tuples, with values between 0 and 255"""
img = Image.new("RGBA", (width, height))
canvas = ImageDraw.Draw(img)
def hsl(x):
to_float = lambda x : x / 255.0
(r, g, b) = map(to_float, x)
h, s, l = colorsys.rgb_to_hsv(r,g,b)
h = h if 0 < h else 1 # 0 -> 1
return h, s, l
rainbow = sorted(colors, key=hsl)
dx = width / float(len(colors))
x = 0
y = height / 2.0
for rgb in rainbow:
canvas.line((x, y, x + dx, y), width=height, fill=rgb)
x += dx
img.show()
However, the result doesn't look very much like a nice rainbow-y spectrum. I suspect I need to either convert to a different color space or handle the HSL triplet differently.
Does anyone know what I need to do to make this data look roughly like a rainbow?
Update:
I was playing around with Hilbert curves and revisited this problem. Sorting the RGB values (same colors in both images) by their position along a Hilbert curve yields an interesting (if still not entirely satisfying) result:
回答1:
You're trying to convert a three-dimensional space into a one-dimensional space. There's no guarantee that you can make a pleasing rainbow out of it, as Oli says.
What you can do is "bucket" the colors into a few different categories based on saturation and value/lightness, and then sort within the categories, to get several independent gradients. For example, high-saturation colors first for the classic rainbow, then mid-saturation high-value colors (pastels), then low-saturation (grays).
Alternately, if all you care about is the rainbow, convert to hsl, then slam saturation to 1.0 and value to 0.5, convert back to rgb and render that instead of the original color.
回答2:
Presumably you are sorting by hue (i.e. H)? That will give a nice result if S and L (or V) are constant, but if they are varying independently, then you will get a bit of a mess!
回答3:
An interesting method for reducing dimensionality of color spaces uses the space-filling Hilbert curve. Two relevant articles are:
- Color Space Dimension Reduction - overview of several methods for reducing dimensionality of color data
- Portrait of the Hilbert Curve - detailed article about Hilbert curves and application to color-space dimensionality reduction
They both consider 3d -> 2d reduction, but the intermediate step of mapping to the 1d curve could be a solution to your problem.
回答4:
Here are some rainbows I made recently, you can modify the idea to do what you want
from PIL import Image, ImageDraw # pip install pillow
import numpy as np
from matplotlib import pyplot as plt
strip_h, strip_w = 100, 720
strip = 255*np.ones((strip_h,strip_w,3), dtype='uint8')
image_val = Image.fromarray(strip)
image_sat = Image.fromarray(strip)
draw0 = ImageDraw.Draw(image_val)
draw1 = ImageDraw.Draw(image_sat)
for y in range(strip_h):
for x in range(strip_w):
draw0.point([x, y], fill='hsl(%d,%d%%,%d%%)'%(x%360,y,50))
draw1.point([x, y], fill='hsl(%d,%d%%,%d%%)'%(x%360,100,y))
plt.subplot(2,1,1)
plt.imshow(image_val)
plt.subplot(2,1,2)
plt.imshow(image_sat)
plt.show()
回答5:
This seems incorrect.
canvas.line((x, y, x + dx, y), width=height, fill=rgb)
Try this.
canvas.rectangle([(x, y), (x+dx, y+height)], fill=rgb)
来源:https://stackoverflow.com/questions/6208980/sorting-a-list-of-rgb-triplets-into-a-spectrum