我有以下透明图像
我想做的是将它们粘贴到具有特定颜色背景的图像上。背景的颜色是随机的,如下所示:
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
有些商标是完全黑色的。海盗船的标志是一个透明背景的全黑标志,但代码没有选择正确的背景
我认为使用像rgb这样的三分量向量机对于随机选择来说是困难的。我会先把它转换成hsv颜色系统(色调、饱和度、亮度)。然后,我们只需要担心色调,就可以使用
random.choice
从可能值的1D列表中选择一个值希望这有帮助。祝你今天愉快
编辑
对于您的函数,它将如下所示:
相关问题 更多 >
编程相关推荐