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


Posted in Python onApril 10, 2014

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杀死一个线程的方法
Sep 06 Python
python中 logging的使用详解
Oct 25 Python
Python字典,函数,全局变量代码解析
Dec 18 Python
python实现画圆功能
Jan 25 Python
Python网络编程之TCP与UDP协议套接字用法示例
Feb 02 Python
Python3 Post登录并且保存cookie登录其他页面的方法
Dec 28 Python
Django Rest framework频率原理与限制
Jul 26 Python
解决python脚本中error: unrecognized arguments: True错误
Apr 20 Python
使用keras实现孪生网络中的权值共享教程
Jun 11 Python
python使用建议与技巧分享(二)
Aug 17 Python
python matplotlib工具栏源码探析二之添加、删除内置工具项的案例
Feb 25 Python
Python3接口性能测试实例代码
Jun 20 Python
Python使用新浪微博API发送微博的例子
Apr 10 #Python
一个检测OpenSSL心脏出血漏洞的Python脚本分享
Apr 10 #Python
Python删除指定目录下过期文件的2个脚本分享
Apr 10 #Python
python实现随机密码字典生成器示例
Apr 09 #Python
Python下的Mysql模块MySQLdb安装详解
Apr 09 #Python
使用python实现递归版汉诺塔示例(汉诺塔递归算法)
Apr 08 #Python
python计算圆周长、面积、球体体积并画出圆
Apr 08 #Python
You might like
PHP4实际应用经验篇(5)
2006/10/09 PHP
mysql_fetch_assoc和mysql_fetch_row的功能加起来就是mysql_fetch_array
2007/01/15 PHP
php利用header函数下载各种文件
2016/08/24 PHP
PHP实现提取多维数组指定一列的方法总结
2019/12/04 PHP
JavaScript高级程序设计(第3版)学习笔记3 js简单数据类型
2012/10/11 Javascript
JavaScript字符串检索字符的方法
2017/06/23 Javascript
理解 Node.js 事件驱动机制的原理
2017/08/16 Javascript
EasyUI Tree树组件无限循环的解决方法
2017/09/27 Javascript
angular4强制刷新视图的方法
2018/10/09 Javascript
vue实现axios图片上传功能
2019/08/20 Javascript
javascript实现倒计时关闭广告
2021/02/09 Javascript
[00:52]DOTA2国际邀请赛
2020/02/21 DOTA
python脚本实现xls(xlsx)转成csv
2016/04/10 Python
解析Python中的生成器及其与迭代器的差异
2016/06/20 Python
Jupyter安装nbextensions,启动提示没有nbextensions库
2020/04/23 Python
Python操作json的方法实例分析
2018/12/06 Python
详解用Python练习画个美队盾牌
2019/03/23 Python
Python可变和不可变、类的私有属性实例分析
2019/05/31 Python
Pytorch 高效使用GPU的操作
2020/06/27 Python
使用Python将语音转换为文本的方法
2020/08/10 Python
python如何提升爬虫效率
2020/09/27 Python
python 如何读、写、解析CSV文件
2021/03/03 Python
捷克领先的户外服装及配件市场零售商:ALPINE PRO
2018/01/09 全球购物
介绍一下javax.servlet.Servlet接口及其主要方法
2015/11/30 面试题
求职简历自荐信
2014/06/18 职场文书
乡镇消防安全责任书
2014/07/23 职场文书
国际政治学专业推荐信
2014/09/26 职场文书
手机被没收的检讨书
2014/10/04 职场文书
学校食堂食品安全承诺书
2015/04/29 职场文书
2015年初三班主任工作总结
2015/05/21 职场文书
2015国庆节放假通知范文
2015/07/30 职场文书
上班旷工检讨书
2015/08/15 职场文书
MySQL复制问题的三个参数分析
2021/04/07 MySQL
Java Optional<Foo>转换成List<Bar>的实例方法
2021/06/20 Java/Android
十大必看国产动漫排名,魁拔上线,第二曾在日本播出
2022/03/18 国漫
CSS 鼠标点击拖拽效果的实现代码
2022/12/24 HTML / CSS