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 相关文章推荐
Python3.2中的字符串函数学习总结
Apr 23 Python
python基于socket实现网络广播的方法
Apr 29 Python
python实现在控制台输入密码不显示的方法
Jul 02 Python
Python实现并行抓取整站40万条房价数据(可更换抓取城市)
Dec 14 Python
python 时间信息“2018-02-04 18:23:35“ 解析成字典形式的结果代码详解
Apr 19 Python
python 定义给定初值或长度的list方法
Jun 23 Python
用python生成与调用cntk模型代码演示方法
Aug 26 Python
pytorch 归一化与反归一化实例
Dec 31 Python
基于Python中random.sample()的替代方案
May 23 Python
基于Python编写一个计算器程序,实现简单的加减乘除和取余二元运算
Aug 05 Python
详解Python openpyxl库的基本应用
Feb 26 Python
python中的plt.cm.Paired用法说明
May 31 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
不用mod_rewrite直接用php实现伪静态化页面代码
2008/10/04 PHP
php正则过滤html标签、空格、换行符的代码(附说明)
2010/10/25 PHP
php实现遍历文件夹的方法汇总
2017/03/02 PHP
js检测客户端不是firefox则提示下载
2007/04/07 Javascript
单击按钮显示隐藏子菜单经典案例
2013/01/04 Javascript
Extjs grid添加一个图片状态或者按钮的方法
2014/04/03 Javascript
JavaScript使用指针操作实现约瑟夫问题实例
2015/04/07 Javascript
信息页文内画中画广告js实现代码(文中加载广告方式)
2016/01/03 Javascript
基于JavaScript实现简单的随机抽奖小程序
2016/01/05 Javascript
EasyUI布局 高度自适应
2016/06/04 Javascript
JavaScript禁止用户多次提交的两种方法
2016/07/24 Javascript
AngularJs Managing Service Dependencies详解
2016/09/02 Javascript
jQuery简单创建节点的方法
2016/09/09 Javascript
强大Vue.js组件浅析
2016/09/12 Javascript
Bootstrap基本插件学习笔记之Alert警告框(20)
2016/12/08 Javascript
javascript表单正则应用
2017/02/04 Javascript
AngularJS select设置默认值的实现方法
2017/08/25 Javascript
JS 实现分页打印功能
2018/05/16 Javascript
Python深入学习之装饰器
2014/08/31 Python
Python 列表list使用介绍
2014/11/30 Python
python安装与使用redis的方法
2016/04/19 Python
Python3 安装PyQt5及exe打包图文教程
2019/01/08 Python
python小项目之五子棋游戏
2019/12/26 Python
Python3基于plotly模块保存图片表格
2020/08/03 Python
python模块内置属性概念及实例
2021/02/18 Python
HTML5 离线应用之打造零请求、无流量网站的解决方法
2013/04/25 HTML / CSS
Stylenanda中文站:韩国一线网络服装品牌
2016/12/22 全球购物
Sarenza德国:法国最大的时尚鞋和包包网上商店
2019/06/08 全球购物
安德玛比利时官网:Under Armour比利时
2019/08/28 全球购物
使用索引有什么好处
2016/07/27 面试题
几个常见的消息中间件(MOM)
2014/01/08 面试题
社区创先争优承诺书
2014/08/30 职场文书
单位综合评价意见
2015/06/05 职场文书
无违反计划生育证明格式
2015/06/24 职场文书
会议室管理制度范本
2015/08/06 职场文书
python某漫画app逆向
2021/03/31 Python