簡體   English   中英

如何生成遠離值列表的隨機值?

[英]How to generate random values that are distant from a list of values?

我有以下透明圖像。

在此處輸入圖像描述 在此處輸入圖像描述 在此處輸入圖像描述 在此處輸入圖像描述

我想要做的是將它們粘貼到具有特定顏色背景的圖像上。 背景的顏色是隨機的,如下所示:

rand1, rand2, rand3 = (random.randint(0, 255),
                       random.randint(0, 255),
                       random.randint(0, 255))

background = Image.new('RGBA', png.size, (rand1, rand2, rand3))

alpha_composite = Image.alpha_composite(background, png)

不幸的是,有些徽標的背景 colors 與 go 不相符。 背景顏色有時會接近徽標內部的顏色,這會使徽標部分或完全不可見。 這是一個背景顏色與 Ubuntu 徽標中的橙色幾乎相同的示例:

在此處輸入圖像描述

我所做的是從每個徽標中獲取所有 colors 並將它們保存在這樣的元組列表中。 這實際上是一個元組列表的列表。 我現在剛剛對其進行了編輯,以突出顯示哪個嵌套的元組列表屬於哪個徽標:

Intel = [(0, 113, 197)]
Corsair = [(4, 7, 7)]
Google = [(66, 133, 244), (234, 67, 53), (251, 188, 5), (52, 168, 83), (0, 255, 255), (255, 128, 0), (255, 255, 0)]
Riot = [(209, 54, 57), (255, 255, 255), (226, 130, 132), (0, 0, 0)]

我想要做的是使用上面的 ^ 信息隨機選擇背景顏色,這樣徽標的任何部分都不會不可見。 我正在尋求有關 go 策略的建議。

這是為徽標添加背景顏色的 function:

def logo_background(path):

    rand1, rand2, rand3 = (random.randint(0, 255),
                           random.randint(0, 255),
                           random.randint(0, 255))

    png = Image.open(path).convert('RGBA')
    colors = extcolors.extract_from_path(path)

    background = Image.new('RGBA', png.size, (rand1, rand2, rand3))

    alpha_composite = Image.alpha_composite(background, png)


    return alpha_composite
>>> extcolors.extract_from_path(path)
[((0, 113, 197), 25727), 56235]

# for the intel logo, which is just blue with transparent background

有些標志是全黑的。 海盜船標志是透明背景的全黑標志,但代碼沒有 select 右背景。

在此處輸入圖像描述

我認為使用像 rgb 這樣的 thre component vektor 很難隨機選擇。 我會先將其轉換為 hsv 顏色系統(色調、飽和度、亮度)。 然后我們只需要擔心色調並且可以使用random.choice從可能值的一維列表中選擇一個值。

Google = [(66, 133, 244), (234, 67, 53), (251, 188, 5), (52, 168, 83), (0, 255, 255), (255, 128, 0), (255, 255, 0)]
threshold = 0.1 #No hue value closer than threshold to a logo-color 

# convert to hsv
GoogleHsv = [colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0) for r,g,b in Google] 
print("GoogleHsv:", GoogleHsv)

# list of possible hue-values that are at least threshold away from all logo-hue-values
choices = [x for x in np.linspace(0,1,201) if min([abs(x-h) for h,l,s in GoogleHsv]) > threshold] 
print("choices:", choices)

h = random.choice(choices)
l = random.random() # random lightness 
s = random.random() # random saturation
# you could also use constants for l,s to alway get a vibrant/dark color.

color = [int(x*255) for x in colorsys.hsv_to_rgb(h, l, s)] # converting back to rbg
print("color:", color)

希望這可以幫助。 祝你今天過得愉快。


編輯

對於您的 function,它看起來像這樣:

def logo_background(path):
    
    threshold = 0.1        
    png = Image.open(path).convert('RGBA')
    used_colors_rgb = extcolors.extract_from_path(path)[0]

    used_hsv = [colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0) for (r,g,b), _ in used_colors_rgb] 
    choices = [x for x in np.linspace(0,1,201) if min([abs(x-h) for h,l,s in used_hsv]) > threshold] 
    h, l, s = random.choice(choices), (random.random()+1) / 2, (random.random()+1) / 2    
    color = [int(x*255) for x in colorsys.hsv_to_rgb(h, l, s)]
  
    background = Image.new('RGBA', png.size,tuple(color))
    alpha_composite = Image.alpha_composite(background, png)
    return alpha_composite
    
logo_background("google.png")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM