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中lambda与def用法对比实例分析
Apr 30 Python
Python入门之三角函数sin()函数实例详解
Nov 08 Python
matplotlib.pyplot画图 图片的二进制流的获取方法
May 24 Python
Flask Web开发入门之文件上传(八)
Aug 17 Python
对python opencv 添加文字 cv2.putText 的各参数介绍
Dec 05 Python
使用Python实现将list中的每一项的首字母大写
Jun 11 Python
Python 静态方法和类方法实例分析
Nov 21 Python
python-视频分帧&多帧合成视频实例
Dec 10 Python
Python实现队列的方法示例小结【数组,链表】
Feb 22 Python
django从后台返回html代码的实例
Mar 11 Python
python3+opencv 使用灰度直方图来判断图片的亮暗操作
Jun 02 Python
Windows环境下Python3.6.8 importError: DLLload failed:找不到指定的模块
Nov 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+mysql 采用ajax技术的 省 市 地 3级联动无刷新菜单 源码
2006/12/16 PHP
一个简单的php加密解密函数(动态加密)
2013/06/19 PHP
php获取远程图片并下载保存到本地的方法分析
2016/10/08 PHP
PHP实现的简单组词算法示例
2018/04/10 PHP
学习jquery必备 api中英文对照的chm手册 下载
2007/05/03 Javascript
Js 获取当前日期时间及其它操作实现代码
2021/03/04 Javascript
初窥JQuery-Jquery简介 入门了解篇
2010/11/25 Javascript
jquery中实现标签切换效果的代码
2011/03/01 Javascript
ASP.NET jQuery 实例4(复制TextBox的文本到本地剪贴板上)
2012/01/13 Javascript
深入理解jQuery中live与bind方法的区别
2013/12/18 Javascript
JavaScrip常见的一些算法总结
2015/12/28 Javascript
JavaScript简单获取页面图片原始尺寸的方法
2016/06/21 Javascript
JavaScript 对象详细整理总结
2016/09/29 Javascript
js省市区级联查询(插件版&无插件版)
2017/03/21 Javascript
Vue项目分环境打包的实现步骤
2018/04/02 Javascript
vue数据操作之点击事件实现num加减功能示例
2019/01/19 Javascript
javascript实现摄像头拍照预览
2019/09/30 Javascript
JS实现点餐自动选择框(案例分析)
2019/12/10 Javascript
在JavaScript中查找字符串中最长单词的三种方法(推荐)
2021/01/18 Javascript
python复制文件代码实现
2013/12/23 Python
使用Python操作Elasticsearch数据索引的教程
2015/04/08 Python
使用python 和 lint 删除项目无用资源的方法
2017/12/20 Python
python中pip的安装与使用教程
2018/08/10 Python
Python查找文件中包含中文的行方法
2018/12/19 Python
Python二元赋值实用技巧解析
2019/10/25 Python
python输入一个水仙花数(三位数) 输出百位十位个位实例
2020/05/03 Python
基于python实现ROC曲线绘制广场解析
2020/06/28 Python
Python异常处理机制结构实例解析
2020/07/23 Python
python+appium+yaml移动端自动化测试框架实现详解
2020/11/24 Python
欧洲第一中国智能手机和平板电脑网上商店:CECT-SHOP
2018/01/08 全球购物
sort命令的作用和用法
2013/08/25 面试题
J2EE中的容器都包括哪些
2013/08/21 面试题
2014年村官工作总结
2014/11/24 职场文书
2014年体育工作总结
2014/11/24 职场文书
解决hive中导入text文件遇到的坑
2021/04/07 Python
springboot如何接收application/x-www-form-urlencoded类型的请求
2021/11/02 Java/Android