Python judge, get a picture of 2 instances of the main hue _python

Source: Internet
Author: User

Python determines the primary hue of a picture, a single color:

Copy Code code 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 a (R, G, b) tuple.
"""

Image = Image.convert (' RGBA ')

# Shrink The image, so we don ' t spend too long analysing color
# frequencies. We don't interpolating so should to 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 * + 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 primary hue of a picture, multiple colors:

Copy Code code 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 a (R, G, b) tuple.
"""

Image = Image.convert (' RGBA ')

# Shrink The image, so we don ' t spend too long analysing color
# frequencies. We don't interpolating so should to 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 * + 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 ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: