如何生成远离值列表的随机值?
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)
不幸的是,一些徽标与其背景颜色不相配。背景颜色有时会接近徽标内部的颜色,这会使徽标部分或完全不可见。这是一个背景颜色几乎与 Ubuntu 徽标中的橙色相同的示例:
我所做的是从每个徽标中获取所有颜色并将它们保存在这样的元组列表中。这实际上是元组列表的列表。我现在刚刚编辑它以突出显示哪个嵌套元组列表属于哪个徽标:
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)]
我想做的是使用上面的 ^ 信息随机选择背景颜色,这样徽标的任何部分都不会被隐藏。我正在征求有关解决此问题的策略的建议..
这是为徽标添加背景颜色的函数:
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 这样的三分量矢量很难随机选择。我会先将它转换为 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)
希望这对您有所帮助。祝你有个愉快的一天。
编辑
对于您的函数,它看起来像这样:
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")
我有以下透明图片。
我想做的是将它们粘贴到具有特定颜色背景的图像上。背景颜色是这样随机的:
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)
不幸的是,一些徽标与其背景颜色不相配。背景颜色有时会接近徽标内部的颜色,这会使徽标部分或完全不可见。这是一个背景颜色几乎与 Ubuntu 徽标中的橙色相同的示例:
我所做的是从每个徽标中获取所有颜色并将它们保存在这样的元组列表中。这实际上是元组列表的列表。我现在刚刚编辑它以突出显示哪个嵌套元组列表属于哪个徽标:
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)]
我想做的是使用上面的 ^ 信息随机选择背景颜色,这样徽标的任何部分都不会被隐藏。我正在征求有关解决此问题的策略的建议..
这是为徽标添加背景颜色的函数:
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 这样的三分量矢量很难随机选择。我会先将它转换为 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)
希望这对您有所帮助。祝你有个愉快的一天。
编辑
对于您的函数,它看起来像这样:
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")