Python Generate random colors (RGB)
Generate random colors (RGB)
I just picked up image processing in python this past week at the suggestion of a friend to generate patterns of random colors. I found this piece of script online that generates a wide array of different colors across the RGB spectrum.
def random_color():
levels = range(32,256,32)
return tuple(random.choice(levels) for _ in range(3))
I am simply interesting in appending this script to only generate one of three random colors. Preferably red, green, and blue.
回答1
A neat way to generate RGB triplets within the 256 (aka 8-byte) range is
color = list(np.random.choice(range(256), size=3))
color
is now a list of size 3 with values in the range 0-255. You can save it in a list to record if the color has been generated before or no.
评论
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
>>> type(vowels)
<class 'list'>
>>> color = list(np.random.choice(range(256), size=3))
>>> type(color)
<class 'list'>
>>> color = tuple(np.random.random(size=3) * 256)
>>> type(color)
<class 'tuple'>
作者:Chuck Lu GitHub |
np
:rand_color = random.choices(range(256), k=3)
color = tuple(np.random.random(size=3) * 256)