Home  >  Article  >  Backend Development  >  Get the main color (color trend) of a picture or logo in python version

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

高洛峰
高洛峰Original
2016-10-18 14:59:051449browse

When you use Google or Baidu to search for pictures, you will find that there is a picture color option. It feels very interesting. Some people may think that this must be artificially divided. Haha, it is possible, but people will probably be exhausted. Open it. It’s a joke, of course it’s through machine recognition. Massive pictures can only be recognized by machines.
Can this function be implemented using python? The answer is: You can do it by using the powerful image processing function of python's PIL module. The code below:

import colorsys

def get_dominant_color(image):

#Color mode conversion for output rgb color value


Image = image.convert('RGBA')

#Generate thumbnails, reduce calculations and CPU pressure

image.thumbnail((200, 200))

max_s core = 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 / 25 5.0)[1]

                                                                                                                 * 4130 + b * 802 + 4096 + 131072) >> 13, 235)

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

          # Ignore the highlight color

                                                                                                                                                                                                                                                                             

                                                                                                                                                                                                                                                                                      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

如何使用:

from PIL import Image

print get_dominant_color(Image.open('logo.jpg'))

This will return an rgb color, but this value is a very precise range, so how do we implement it What about the color gamut like Baidu pictures? ?

In fact, the method is very simple. r/g/b are all values ​​from 0-255. We only need to divide these three values ​​into equal intervals, and then combine them to obtain approximate values. For example: divide it into 0-127, and 128-255, and then combine them freely. Eight combinations can appear, and then just pick out the more representative colors from them.

Of course, I am just giving an example. You can also divide it more finely, so that the displayed colors will be more accurate~~ Let’s try it now

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:descendants of pythonNext article:descendants of python