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 相关文章推荐
在ironpython中利用装饰器执行SQL操作的例子
May 02 Python
举例讲解Python中字典的合并值相加与异或对比
Jun 04 Python
神经网络(BP)算法Python实现及应用
Apr 16 Python
python异步存储数据详解
Mar 19 Python
numpy concatenate数组拼接方法示例介绍
May 27 Python
python制作简单五子棋游戏
Jun 18 Python
Pyqt5 实现跳转界面并关闭当前界面的方法
Jun 19 Python
Python的垃圾回收机制详解
Aug 28 Python
pycharm 2019 最新激活方式(pycharm破解、激活)
Sep 22 Python
Python如何向SQLServer存储二进制图片
Jun 08 Python
Python获取指定日期是"星期几"的6种方法
Mar 13 Python
Pyhton爬虫知识之正则表达式详解
Apr 01 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获取通过http协议post提交过来xml数据及解析xml
2012/12/16 PHP
input file获得文件根目录简单实现
2013/04/26 PHP
php实现的支持imagemagick及gd库两种处理的缩略图生成类
2014/09/23 PHP
PHP中把对象数组转换成普通数组的方法
2015/07/10 PHP
php arsort 数组降序排序详细介绍
2016/11/17 PHP
验证用户是否修改过页面的数据的实现方法
2008/09/26 Javascript
javascript之typeof、instanceof操作符使用探讨
2013/05/19 Javascript
JSCode all of Brower 全局屏蔽网页右键功能 具体实现
2013/06/05 Javascript
自己封装的常用javascript函数分享
2015/01/07 Javascript
JavaScript中的方法重载实例
2015/03/16 Javascript
js实现拖拽效果(构造函数)
2015/12/14 Javascript
jQuery回到顶部的代码
2016/07/09 Javascript
artDialog+plupload实现多文件上传
2016/07/19 Javascript
shiro授权的实现原理
2017/09/21 Javascript
vue.js实例对象+组件树的详细介绍
2017/10/20 Javascript
JS简单实现动态添加HTML标记的方法示例
2018/04/08 Javascript
vue中v-show和v-if的异同及v-show用法
2019/06/06 Javascript
解决vue.js提交数组时出现数组下标的问题
2019/11/05 Javascript
小程序跨页面交互的作用与方法详解
2020/01/07 Javascript
javascript递归函数定义和用法示例分析
2020/07/22 Javascript
python实现telnet客户端的方法
2015/04/15 Python
编写Python脚本使得web页面上的代码高亮显示
2015/04/24 Python
Python2.7+pytesser实现简单验证码的识别方法
2017/12/29 Python
python pandas 如何替换某列的一个值
2018/06/09 Python
Python开启线程,在函数中开线程的实例
2019/02/22 Python
python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据)
2019/08/28 Python
关于python中的xpath解析定位
2020/03/06 Python
Python中pass的作用与使用教程
2020/11/13 Python
Django利用elasticsearch(搜索引擎)实现搜索功能
2020/11/26 Python
GLAMGLOW香港官网:明星出镜前的秘密武器
2017/03/16 全球购物
高中生学期学习自我评价
2014/02/24 职场文书
《闻一多先生的说和做》教学反思
2014/04/28 职场文书
个人年终总结开头
2015/03/06 职场文书
2016年会领导致辞稿
2015/07/29 职场文书
土木工程生产实习心得体会
2016/01/22 职场文书
python3实现Dijkstra算法最短路径的实现
2021/05/12 Python