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 + hadoop streaming 分布式编程(一) -- 原理介绍,样例程序与本地调试
Jul 14 Python
Python中使用SAX解析xml实例
Nov 21 Python
举例讲解Python程序与系统shell交互的方式
Apr 09 Python
python实现多线程的两种方式
May 22 Python
python搭建虚拟环境的步骤详解
Sep 27 Python
matplotlib作图添加表格实例代码
Jan 23 Python
python如何求解两数的最大公约数
Sep 27 Python
Python实现的登录验证系统完整案例【基于搭建的MVC框架】
Apr 12 Python
Python字符串内置函数功能与用法总结
Apr 16 Python
Python3将ipa包中的文件按大小排序
Apr 17 Python
利用Matlab绘制各类特殊图形的实例代码
Jul 16 Python
Python pyecharts绘制条形图详解
Apr 02 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的开合式多级菜单程序
2006/10/09 PHP
PHP个人网站架设连环讲(四)
2006/10/09 PHP
php中设置多级目录session的问题
2011/08/08 PHP
PHP类的封装与继承详解
2015/09/29 PHP
PHP实现截取中文字符串不出现?号的解决方法
2016/12/29 PHP
PHP实现基于状态的责任链审批模式详解
2019/05/31 PHP
JQuery 图片延迟加载并等比缩放插件
2009/11/09 Javascript
jQuery实战之品牌展示列表效果
2011/04/10 Javascript
原生Js实现元素渐隐/渐现(原理为修改元素的css透明度)
2013/06/24 Javascript
编写简单的jQuery提示插件
2014/12/21 Javascript
innerHTML中标签可以换行的方法汇总
2015/08/14 Javascript
jQuery Timelinr实现垂直水平时间轴插件(附源码下载)
2016/02/16 Javascript
JavaScript组合模式学习要点
2016/08/26 Javascript
vue实现添加标签demo示例代码
2017/01/21 Javascript
使用js获取伪元素的content实例
2017/10/24 Javascript
vue 之 css module的使用方法
2018/12/04 Javascript
Node 搭建一个静态资源服务器的实现
2019/05/20 Javascript
JS计算两个数组的交集、差集、并集、补集(多种实现方式)
2019/05/21 Javascript
layui 实现二级弹窗弹出之后 关闭一级弹窗的方法
2019/09/18 Javascript
[03:10]超级美酒第四天 fy拉比克秀 大合集
2018/06/05 DOTA
python dataframe astype 字段类型转换方法
2018/04/11 Python
深入分析python中整型不会溢出问题
2018/06/18 Python
关于python2 csv写入空白行的问题
2018/06/22 Python
Python实现App自动签到领取积分功能
2018/09/29 Python
Python实现的爬取小说爬虫功能示例
2019/03/30 Python
Python3-异步进程回调函数(callback())介绍
2020/05/02 Python
Html5新增标签与样式及让元素水平垂直居中
2019/07/11 HTML / CSS
Speedo澳大利亚官网:全球领先游泳品牌
2018/02/04 全球购物
AutoShack.com加拿大:北美主要的汽车零部件零售商
2019/07/24 全球购物
汽车维修工岗位职责
2014/02/12 职场文书
入党积极分子自我鉴定
2014/02/18 职场文书
绩效工资实施方案
2014/03/15 职场文书
党建工作先进材料
2014/05/02 职场文书
出纳试用期工作总结2015
2015/05/28 职场文书
2019年幼儿园家长接送责任书
2019/10/29 职场文书
Pygame Time时间控制的具体使用详解
2021/11/17 Python