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 THREADING模块中的JOIN()方法深入理解
Feb 18 Python
python简单文本处理的方法
Jul 10 Python
Python的Scrapy爬虫框架简单学习笔记
Jan 20 Python
Python3使用requests包抓取并保存网页源码的方法
Mar 15 Python
Python 多核并行计算的示例代码
Nov 07 Python
Python3 Post登录并且保存cookie登录其他页面的方法
Dec 28 Python
PyCharm导入python项目并配置虚拟环境的教程详解
Oct 13 Python
python 通过视频url获取视频的宽高方式
Dec 10 Python
Python3.6 中的pyinstaller安装和使用教程
Mar 16 Python
基于Python实现视频的人脸融合功能
Jun 12 Python
PyCharm+PyQt5+QtDesigner配置详解
Aug 12 Python
Python中threading库实现线程锁与释放锁
May 17 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 获取百度的热词数据的代码
2012/02/18 PHP
thinkPHP简单遍历数组方法分析
2016/05/16 PHP
php微信公众号开发(2)百度BAE搭建和数据库使用
2016/12/15 PHP
php微信开发之关键词回复功能
2018/06/13 PHP
利用JQuery和Servlet实现跨域提交请求示例分享
2014/02/12 Javascript
手写的一个兼容各种浏览器的javascript getStyle函数(获取元素的样式)
2014/06/06 Javascript
Javascript的无new构建实例详解
2016/05/15 Javascript
星期几的不同脚本写法(推荐)
2016/06/01 Javascript
微信小程序 教程之模块化
2016/10/17 Javascript
AngularJS动态绑定HTML的方法分析
2016/11/07 Javascript
vue element-ui 绑定@keyup事件无效的解决方法
2018/03/09 Javascript
VUE脚手架具体使用方法
2019/05/20 Javascript
详解vue高级特性
2020/06/09 Javascript
Python标准库之随机数 (math包、random包)介绍
2014/11/25 Python
Python中实现对list做减法操作介绍
2015/01/09 Python
python 自动化将markdown文件转成html文件的方法
2016/09/23 Python
python算法演练_One Rule 算法(详解)
2017/05/17 Python
python实现壁纸批量下载代码实例
2018/01/25 Python
python实现复制文件到指定目录
2019/10/16 Python
Python hmac模块使用实例解析
2019/12/24 Python
如何基于python测量代码运行时间
2019/12/25 Python
Matplotlib scatter绘制散点图的方法实现
2020/01/02 Python
TensorFlow查看输入节点和输出节点名称方式
2020/01/04 Python
pyftplib中文乱码问题解决方案
2020/01/11 Python
python实现随机加减法生成器
2020/02/24 Python
python 6种方法实现单例模式
2020/12/15 Python
美国著名首饰网站:BaubleBar
2016/08/29 全球购物
Room Mate Hotels美国:西班牙酒店品牌
2018/04/10 全球购物
意大利奢侈品多品牌集合店:TheDoubleF
2019/08/24 全球购物
PHP使用Redis队列执行定时任务实例讲解
2021/03/24 PHP
机电专业大学生求职信
2013/10/04 职场文书
群教班子对照检查材料
2014/08/26 职场文书
歼十出击观后感
2015/06/11 职场文书
分享:关于学习的励志名言赏析
2019/08/16 职场文书
创业计划书之都市休闲农庄
2019/12/28 职场文书
vue.js Router中嵌套路由的实用示例
2021/06/27 Vue.js