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 26 Python
Python django实现简单的邮件系统发送邮件功能
Jul 14 Python
python实现学生管理系统
Jan 11 Python
纯python实现机器学习之kNN算法示例
Mar 01 Python
Python实现的文本对比报告生成工具示例
May 22 Python
Python常用特殊方法实例总结
Mar 22 Python
Django中自定义admin Xadmin的实现代码
Aug 09 Python
django写用户登录判定并跳转制定页面的实例
Aug 21 Python
解决torch.autograd.backward中的参数问题
Jan 07 Python
Django获取model中的字段名和字段的verbose_name方式
May 19 Python
删除pycharm鼠标右键快捷键打开项目的操作
Jan 16 Python
Python基础之进程详解
May 21 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
PHP编辑器PhpStrom运行缓慢问题
2017/02/21 PHP
js之WEB开发调试利器:Firebug 下载
2007/01/13 Javascript
JavaScript URL参数读取改进版
2009/01/16 Javascript
Javascript 文件夹选择框的两种解决方案
2009/07/01 Javascript
网页中CDATA标记的说明
2010/09/12 Javascript
js实现屏蔽默认快捷键调用自定义事件示例
2013/06/18 Javascript
jQuery中append()方法用法实例
2014/12/25 Javascript
AngularJS语法详解
2015/01/23 Javascript
javascript委托(Delegate)blur和focus用法实例分析
2015/05/26 Javascript
jQuery绑定事件监听bind和移除事件监听unbind用法实例详解
2016/01/19 Javascript
JS 实现随机验证码功能
2017/02/15 Javascript
JS图片预加载插件详解
2017/06/21 Javascript
基于Vue实现拖拽功能
2020/07/29 Javascript
webpack打包nodejs项目的方法
2018/09/26 NodeJs
微信小程序保持session会话的方法
2020/03/20 Javascript
javascript执行上下文、变量对象实例分析
2020/04/25 Javascript
python的变量与赋值详细分析
2017/11/08 Python
使用Pyinstaller转换.py文件为.exe可执行程序过程详解
2019/08/06 Python
PyCharm 2019.3发布增加了新功能一览
2019/12/08 Python
python3检查字典传入函数键是否齐全的实例
2020/06/05 Python
基于PyTorch的permute和reshape/view的区别介绍
2020/06/18 Python
4款Python 类型检查工具,你选择哪个呢?
2020/10/30 Python
Python导入父文件夹中模块并读取当前文件夹内的资源
2020/11/19 Python
Python爬虫爬取有道实现翻译功能
2020/11/27 Python
Omio意大利:全欧洲低价大巴、火车和航班搜索和比价
2017/12/02 全球购物
应届生人事助理求职信
2013/11/09 职场文书
外贸业务员的岗位职责
2013/11/23 职场文书
大学团支书的自我评价分享
2013/12/14 职场文书
预备党员政审材料
2014/02/04 职场文书
职业生涯规划书范文
2014/03/10 职场文书
2015年优质护理服务工作总结
2015/04/08 职场文书
公司辞职信模板
2015/05/13 职场文书
初中家长意见
2015/06/03 职场文书
使用pandas或numpy处理数据中的空值(np.isnan()/pd.isnull())
2021/05/14 Python
Python+Tkinter打造签名设计工具
2022/04/01 Python
Mysql 8.x 创建用户以及授予权限的操作记录
2022/04/18 MySQL