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程序员
Jun 12 Python
Python统计日志中每个IP出现次数的方法
Jul 06 Python
Python脚本实现12306火车票查询系统
Sep 30 Python
python实现的正则表达式功能入门教程【经典】
Jun 05 Python
Python 比较两个数组的元素的异同方法
Aug 17 Python
Python3实现简单可学习的手写体识别(实例讲解)
Oct 21 Python
简单谈谈python中的lambda表达式
Jan 19 Python
python3监控CentOS磁盘空间脚本
Jun 21 Python
pygame游戏之旅 创建游戏窗口界面
Nov 20 Python
python实现遍历文件夹图片并重命名
Mar 23 Python
python+appium+yaml移动端自动化测试框架实现详解
Nov 24 Python
python 管理系统实现mysql交互的示例代码
Dec 06 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旋转图片90度的方法
2013/11/07 PHP
PHP同时连接多个mysql数据库示例代码
2014/03/17 PHP
Laravel框架路由设置与使用示例
2018/06/12 PHP
按钮接受回车事件的三种实现方法
2014/06/06 Javascript
JavaScript常用小技巧小结
2014/12/29 Javascript
深入理解JavaScript系列(29):设计模式之装饰者模式详解
2015/03/03 Javascript
js+css实现上下翻页相册代码分享
2015/08/18 Javascript
深入理解MVC中的时间js格式化
2016/05/19 Javascript
javascript实现简单的on事件绑定
2016/08/23 Javascript
详解vuelidate 对于vueJs2.0的验证解决方案
2017/03/09 Javascript
Mongoose经常返回e11000 error的原因分析
2017/03/29 Javascript
Angular在模板驱动表单中自定义校验器的方法
2017/08/09 Javascript
Angular自定义组件实现数据双向数据绑定的实例
2017/12/11 Javascript
jquery radio 动态控制选中失效问题的解决方法
2018/02/28 jQuery
Vue传参一箩筐(页面、组件)
2019/04/04 Javascript
浅谈javascript事件环微任务和宏任务队列原理
2020/09/12 Javascript
ES11新增的这9个新特性,你都掌握了吗
2020/10/15 Javascript
vue+iview实现分页及查询功能
2020/11/17 Vue.js
addEventListener()和removeEventListener()追加事件和删除追加事件
2020/12/04 Javascript
[47:46]完美世界DOTA2联赛 Magma vs GXR 第三场 11.07
2020/11/10 DOTA
从Python的源码浅要剖析Python的内存管理
2015/04/16 Python
python dict.get()和dict['key']的区别详解
2016/06/30 Python
Python语言实现百度语音识别API的使用实例
2017/12/13 Python
Python 从一个文件中调用另一个文件的类方法
2019/01/10 Python
Python中栈、队列与优先级队列的实现方法
2019/06/30 Python
对Pytorch中Tensor的各种池化操作解析
2020/01/03 Python
Python使用GitPython操作Git版本库的方法
2020/02/29 Python
如何基于Python爬虫爬取美团酒店信息
2020/11/03 Python
利用python查看数组中的所有元素是否相同
2021/01/08 Python
JPA的特点
2014/10/25 面试题
什么是GWT的Module
2013/01/20 面试题
怎样有效的进行自我评价
2013/10/06 职场文书
幼儿园教研活动方案
2014/01/19 职场文书
《中国的气候》教学反思
2014/02/23 职场文书
党员干部反四风民主生活会对照检查材料思想汇报
2014/10/12 职场文书
详解如何使用Node.js实现热重载页面
2021/05/06 Javascript