Get a picture or logo of the main color (color trend) Python version

Source: Internet
Author: User
In the use of Google or Baidu search will find a picture color options, it feels very interesting, some people may think this is definitely artificial to divide, hehe, there is this possibility, but estimated that people will be exhausted, open a joke, of course, is through the machine recognition, a huge amount of pictures only machine recognition to do.
Is it possible to do this with python? The answer is: can

With the powerful image processing capabilities of the Python PIL module, the following code is available:

Import Colorsys

def get_dominant_color (image):


#颜色模式转换 in order to output RGB color values

Image = Image.convert (' RGBA ')

#生成缩略图, reduce computational load and reduce CPU pressure

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 Pure Black

If a = = 0:

Continue

saturation = COLORSYS.RGB_TO_HSV (r/255.0, g/255.0, b/255.0) [1]

y = min (abs (R * 2104 + G * 4130 + b * 802 + 4096 + 131072) >> 13, 235)

y = (y-16.0)/(235-16)

# Ignore High 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

How to use:

From PIL import Image

Print Get_dominant_color (Image.open (' logo.jpg '))

This will return an RGB color, but this value is very accurate range, then how do we achieve the color gamut of Baidu image??
In fact, the method is very simple, r/g/b are 0-255 of the value, we just have to divide the three values into equal intervals, and then combine, take the approximate value. For example: Divided into 0-127, and 128-255, and then free combination, can appear eight combinations, and then choose a more representative color.
Of course I just give an example, you can also divide the finer, so that the color of the display will be more accurate ~ ~ Everybody try it quickly

  • 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.