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中使用成员运算符的示例
May 13 Python
Python之web模板应用
Dec 26 Python
python编程测试电脑开启最大线程数实例代码
Feb 09 Python
python搭建服务器实现两个Android客户端间收发消息
Apr 12 Python
python 读取文本文件的行数据,文件.splitlines()的方法
Jul 12 Python
详解用python生成随机数的几种方法
Aug 04 Python
python爬虫-模拟微博登录功能
Sep 12 Python
解决python 找不到module的问题
Feb 12 Python
Python交互环境下打印和输入函数的实例内容
Feb 16 Python
Python 实现国产SM3加密算法的示例代码
Sep 21 Python
浅谈Python响应式类库RxPy
Jun 14 Python
Python批量解压&压缩文件夹的示例代码
Apr 04 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
Linux中为php配置伪静态
2014/12/17 PHP
smarty模板引擎中变量及变量修饰器用法实例
2015/01/22 PHP
PHP实现查询两个数组中不同元素的方法
2016/02/23 PHP
php使用imagecopymerge()函数创建半透明水印
2018/01/25 PHP
学习ExtJS TextField常用方法
2009/10/07 Javascript
jQuery随机切换图片的小例子
2013/04/18 Javascript
JavaScript三元运算符的多种使用技巧
2015/04/16 Javascript
javascript跨域方法、原理以及出现问题解决方法(详解)
2015/08/06 Javascript
jQuery热气球动画半透明背景的后台登录界面代码分享
2015/08/28 Javascript
JavaScript实现输入框(密码框)出现提示语
2016/01/12 Javascript
Angular下H5上传图片的方法(可多张上传)
2017/01/09 Javascript
BootStrapValidator初使用教程详解
2017/02/10 Javascript
微信小程序 弹框和模态框实现代码
2017/03/10 Javascript
Vue.js常用指令的使用小结
2017/06/23 Javascript
iView框架问题整理小结
2018/10/16 Javascript
JSON是什么?有哪些优点?JSON和XML的区别?
2019/04/29 Javascript
js观察者模式的弹幕案例
2020/11/23 Javascript
Python 除法小技巧
2008/09/06 Python
Python的pycurl包用法简介
2015/11/13 Python
Python实现类似jQuery使用中的链式调用的示例
2016/06/16 Python
python中logging库的使用总结
2017/10/18 Python
Python读取mat文件,并保存为pickle格式的方法
2018/10/23 Python
wxPython实现文本框基础组件
2019/11/18 Python
python对文件的操作方法汇总
2020/02/28 Python
Pyspark获取并处理RDD数据代码实例
2020/03/27 Python
Python pymsql模块的使用
2020/09/07 Python
html5 input属性使用示例
2013/06/28 HTML / CSS
南京某公司笔试题
2013/01/27 面试题
简历中自我评价范文3则
2013/12/14 职场文书
中考标语大全
2014/06/05 职场文书
自强自立美德少年事迹材料
2014/08/16 职场文书
获奖感言范文
2015/07/31 职场文书
创业计划书之个人工作室
2019/08/22 职场文书
pytorch 如何使用batch训练lstm网络
2021/05/28 Python
Python中的datetime包与time包包和模块详情
2022/02/28 Python