python判断、获取一张图片主色调的2个实例

 更新时间:2014年04月10日 14:51:19   作者:  
一幅图片,想通过程序判断获得其主要色调,应该怎么样处理?本文通过python实现判断、获取一张图片的主色调方法,需要的朋友可以参考下

python判断图片主色调,单个颜色:

复制代码 代码如下:

#!/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 analysing color
# frequencies. We're not interpolating so should 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判断一张图片的主色调,多个颜色:

复制代码 代码如下:

#!/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 analysing color
# frequencies. We're not interpolating so should 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()

 

相关文章

  • 在Python中定义一个常量的方法

    在Python中定义一个常量的方法

    今天小编就为大家分享一篇在Python中定义一个常量的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-11-11
  • 在pycharm中配置Anaconda以及pip源配置详解

    在pycharm中配置Anaconda以及pip源配置详解

    这篇文章主要介绍了在pycharm中配置Anaconda以及pip源配置详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • python游戏实战项目之智能五子棋简易版

    python游戏实战项目之智能五子棋简易版

    利用Python实现智能五子棋,实现之后发现我玩不赢它!本篇为你带来用python编写的五子棋小游戏,文中给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值
    2021-09-09
  • pytorch程序异常后删除占用的显存操作

    pytorch程序异常后删除占用的显存操作

    今天小编就为大家分享一篇pytorch程序异常后删除占用的显存操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • python之关于数组和列表的区别及说明

    python之关于数组和列表的区别及说明

    这篇文章主要介绍了python之关于数组和列表的区别及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • 构建可视化 web的 Python 神器streamlit

    构建可视化 web的 Python 神器streamlit

    这篇文章主要介绍了构建可视化web的Python神器streamlit,Streamlit是一个用于机器学习、数据可视化的Python框架,它能几行代码就构建出一个精美的在线app应用
    2022-06-06
  • python实现带声音的摩斯码翻译实现方法

    python实现带声音的摩斯码翻译实现方法

    这篇文章主要介绍了python实现带声音的摩斯码翻译实现方法,涉及pygame模块操作及摩斯码实现技巧,需要的朋友可以参考下
    2015-05-05
  • python实现按首字母分类查找功能

    python实现按首字母分类查找功能

    这篇文章主要介绍了python实现按首字母分类查找功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • Python使用matplotlib实现绘制自定义图形功能示例

    Python使用matplotlib实现绘制自定义图形功能示例

    这篇文章主要介绍了Python使用matplotlib实现绘制自定义图形功能,结合实例形式分析了Python基于matplotlib模块实现自定义图形绘制相关操作技巧,需要的朋友可以参考下
    2018-01-01
  • 对python中数据集划分函数StratifiedShuffleSplit的使用详解

    对python中数据集划分函数StratifiedShuffleSplit的使用详解

    今天小编就为大家分享一篇对python中数据集划分函数StratifiedShuffleSplit的使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12

最新评论