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实现批量读取word中表格信息的方法
Jul 30 Python
用Python编写简单的微博爬虫
Mar 04 Python
Python多进程同步简单实现代码
Apr 27 Python
简单讲解Python编程中namedtuple类的用法
Jun 21 Python
Python中多线程的创建及基本调用方法
Jul 08 Python
使用pandas实现csv/excel sheet互相转换的方法
Dec 10 Python
不到40行代码用Python实现一个简单的推荐系统
May 10 Python
python urllib爬虫模块使用解析
Sep 05 Python
python 实现将Numpy数组保存为图像
Jan 09 Python
Python表达式的优先级详解
Feb 18 Python
Python3之外部文件调用Django程序操作model等文件实现方式
Apr 07 Python
Python Pandas数据分析之iloc和loc的用法详解
Nov 11 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
使用 MySQL Date/Time 类型
2008/03/26 PHP
php 数组的一个悲剧?
2011/05/11 PHP
php动态添加url查询参数的方法
2015/04/14 PHP
php支持断点续传、分块下载的类
2016/05/02 PHP
PHP设计模式(一)工厂模式Factory实例详解【创建型】
2020/05/02 PHP
用JavaScrpt实现文件夹简单轻松加密的实现方法图文
2008/09/08 Javascript
Javascript解决常见浏览器兼容问题的12种方法
2010/01/04 Javascript
jquery photoFrame 图片边框美化显示插件
2010/06/28 Javascript
前端开发的开始---基于面向对象的Ajax类
2010/09/17 Javascript
在IE 浏览器中使用 jquery的fadeIn() 效果 英文字符字体加粗
2011/06/02 Javascript
JS根据生日算年龄的方法
2015/05/05 Javascript
clipboard.js无需Flash无需依赖任何JS库实现文本复制与剪切
2015/10/10 Javascript
20行js代码实现的贪吃蛇小游戏
2017/06/20 Javascript
Popup弹出框添加数据实现方法
2017/10/27 Javascript
jQuery实现点击DIV同时点击CheckBox,并为DIV上背景色的实例
2017/12/18 jQuery
JavaScript树的深度优先遍历和广度优先遍历算法示例
2018/07/30 Javascript
jQuery+ajax实现批量删除功能完整示例
2019/06/06 jQuery
JS+CSS实现随机点名(实例代码)
2019/11/04 Javascript
vue子组件改变父组件传递的prop值通过sync实现数据双向绑定(DEMO)
2020/02/01 Javascript
利用JavaScript模拟京东按键输入功能
2020/12/01 Javascript
python实现12306火车票查询器
2017/04/20 Python
Python实现的井字棋(Tic Tac Toe)游戏示例
2018/01/31 Python
基于python进行桶排序与基数排序的总结
2018/05/29 Python
PyQt Qt Designer工具的布局管理详解
2019/08/07 Python
Python实现清理微信僵尸粉功能示例【基于itchat模块】
2020/05/29 Python
python生成xml时规定dtd实例方法
2020/09/21 Python
李宁官方网店:中国运动品牌
2017/11/02 全球购物
数控机械专业个人的自我评价
2014/01/02 职场文书
酒店led欢迎词
2014/01/09 职场文书
旅游专业职业生涯规划范文
2014/01/13 职场文书
农民工工资发放承诺书
2014/03/31 职场文书
大学生应聘导游自荐信
2014/06/02 职场文书
暑期教师培训方案
2014/06/07 职场文书
校长四风对照检查材料
2014/09/27 职场文书
2015年社区宣传工作总结
2015/05/20 职场文书
求职自我评价参考范文
2019/05/16 职场文书