python判断获取一张图片主色调的2个实例 python判断、获取一张图片主色调的2个实例 作者: 字体:[增加 减小] 类型:转载 一幅图片,想通过程序判断获得其主要色调,应该怎么样处理?本文通过python实现判断、获取一张图片的主色调方法,需要的朋友可以参考下 python判断图片主色调,单个颜色: 复制代码 代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- import colorsys from PIL import Image import optparse def get_dominant_color(image): """ Find a PIL image's dominant color, returning an (r, g, b) tuple. """ image = ('RGBA') # Shrink the image, so we don't spend too long analysing color # frequencies. We're not interpolating so should be quick. ((200, 200)) max_score = None dominant_color = None for count, (r, g, b, a) in ([0] * [1]): # Skip 100% transparent pixels if a == 0: continue # Get color saturation, 0-1 saturation = (r / , g / , b / )[1] # Calculate luminance - integer YUV conversion from # y = min(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >> 13, 235) # Rescale luminance from 16-235 to 0-1 y = (y - ) / (235 - 16) # Ignore the brightest colors if y > : continue # Calculate the score, preferring highly saturated colors. # Add to the saturation so we don't completely ignore grayscale # colors by multiplying the count by zero, but still give them a low # weight. score = (saturation + ) * count if score