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

     评论

    Should be color = list(np.random.random(size=3) * 256) Mar 6, 2018 at 16:48
     
    Or without np: rand_color = random.choices(range(256), k=3) Jul 5, 2021 at 23:33
     
     
    You could rather use a tuple: color = tuple(np.random.random(size=3) * 256) May 19 at 8:58

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

  • 相关阅读:
    关于产品
    Windows服务 + Quartz.NET
    C#
    C#
    URL
    前端生态系统总结
    字符串相似度
    Process Explore & Windbg
    webpack
    JS
  • 原文地址:https://www.cnblogs.com/chucklu/p/16703247.html
Copyright © 2020-2023  润新知