Python determines the Dominant Color of an image, with a single color:
Copy codeThe Code is as follows:
#! /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 = image. convert ('rgba ')
# Shrink the image, so we don't spend too long analyzing color
# Frequencies. We're not interpolating so shocould be quick.
Image. thumbnail (200,200 ))
Max_score = None
Dominant_color = None
For count, (r, g, B, a) in image. getcolors (image. size [0] * image. size [1]):
# Skip 100% transparent pixels
If a = 0:
Continue
# Get color saturation, 0-1
Saturation = colorsys. rgb_to_hsv (r/255.0, g/255.0, B/255.0) [1]
# Calculate luminance-integer YUV conversion from
# Http://en.wikipedia.org/wiki/YUV
Y = min (abs (r * 2104 + g * 4130 + B * 802 + 4096 + 131072)> 13,235)
# Rescale luminance from 16-235 to 0-1
Y = (y-16.0)/(235-16)
# Ignore the brightest colors
If y> 0.9:
Continue
# Calculate the score, preferring highly saturated colors.
# Add 0.1 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 + 0.1) * count
If score> max_score:
Max_score = score
Dominant_color = (r, g, B)
Return dominant_color
Def main ():
Img = Image. open ("meitu.jpg ")
Print '# % 02x % 02x % 02x' % get_dominant_color (img)
If _ name _ = '_ main __':
Main ()
Python determines the Dominant Color of an image. Multiple colors:
Copy codeThe Code is as follows:
#! /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 = image. convert ('rgba ')
# Shrink the image, so we don't spend too long analyzing color
# Frequencies. We're not interpolating so shocould be quick.
# Image. thumbnail (200,200 ))
Max_score = 1
Dominant_color = []
For count, (r, g, B, a) in image. getcolors (image. size [0] * image. size [1]):
# Skip 100% transparent pixels
If a = 0:
Continue
# Get color saturation, 0-1
Saturation = colorsys. rgb_to_hsv (r/255.0, g/255.0, B/255.0) [1]
# Calculate luminance-integer YUV conversion from
# Http://en.wikipedia.org/wiki/YUV
Y = min (abs (r * 2104 + g * 4130 + B * 802 + 4096 + 131072)> 13,235)
# Rescale luminance from 16-235 to 0-1
Y = (y-16.0)/(235-16)
# Ignore the brightest colors
If y> 0.9:
Continue
# Calculate the score, preferring highly saturated colors.
# Add 0.1 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 + 0.1) * count
If score> max_score:
Max_score = score
Dominant_color.append (r, g, B ))
Return dominant_color
Def main ():
Img = Image. open ("meitu.jpg ")
Colors = get_dominant_color (img)
For item in colors:
Print '# % 02x % 02x % 02x' % item
If _ name _ = '_ main __':
Main ()