Python uses PIL to get the main color of the picture and compare it with the color library _python

Source: Internet
Author: User
Tags color gamut

This example describes how Python can get the main color of a picture and compare it with a color library by PIL. Share to everyone for your reference. The specific analysis is as follows:

This code is mainly used to extract the main color from the picture, similar to Goolge and Baidu's image search can be specified according to the color search, so we first need to extract the main color of each picture, and then divide the color to its closest color segment, then you can follow the color search.

You will find a picture color option when you use Google or Baidu to search for images. Feeling very interesting, some people may think this is definitely man-made to divide, oh, there is this possibility, but it is estimated that people will be exhausted, a joke, of course, by machine recognition, a large amount of pictures only machine recognition can do.

Can that be achieved with Python? The answer is: can

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

Copy Code code as follows:
Import Colorsys
def get_dominant_color (image):
#颜色模式转换 to output RGB color values
Image = Image.convert (' RGBA ')
#生成缩略图, reduce the amount of calculation, reduce the 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 * + 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 precise range, then how do we achieve the color gamut of Baidu picture??

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 combined to take an approximate value. For example: Divided into 0-127, and 128-255, then free combination, can appear eight combinations, and then pick out the more representative color.

Of course I just give an example, you can also be divided into finer, so that the color will be more accurate ~ ~ Let's try it quickly

I hope this article will help you with your Python programming.

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.

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.