Python judges and obtains two instances of the Dominant Color of an image.

Source: Internet
Author: User

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 ()

 

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.