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算法学习之计数排序实例
Dec 18 Python
Python自动化测试工具Splinter简介和使用实例
May 13 Python
深入理解Python中各种方法的运作原理
Jun 15 Python
5种Python单例模式的实现方式
Jan 14 Python
对比Python中__getattr__和 __getattribute__获取属性的用法
Jun 21 Python
python 类详解及简单实例
Mar 24 Python
Python random模块制作简易的四位数验证码
Feb 01 Python
CentOS 7如何实现定时执行python脚本
Jun 24 Python
基于python图书馆管理系统设计实例详解
Aug 05 Python
浅析python 通⽤爬⾍和聚焦爬⾍
Sep 28 Python
Django通过设置CORS解决跨域问题
Nov 26 Python
python 实现数据库中数据添加、查询与更新的示例代码
Dec 07 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
探讨:如何使用PhpDocumentor生成文档
2013/06/25 PHP
php中的静态变量的基本用法
2014/03/20 PHP
Zend Framework 2.0事件管理器(The EventManager)入门教程
2014/08/11 PHP
基于Codeigniter框架实现的student信息系统站点动态发布功能详解
2017/03/23 PHP
laravel 使用auth编写登录的方法
2019/09/30 PHP
经验几则 推荐
2006/09/05 Javascript
javascript 学习笔记(一)DOM基本操作
2011/04/08 Javascript
将光标定位于输入框最右侧实现代码
2012/12/04 Javascript
关于IE中getElementsByClassName不能用的问题解决方法
2013/08/26 Javascript
jQuery中replaceWith()方法用法实例
2014/12/25 Javascript
JSON格式的时间/Date(2367828670431)/格式转为正常的年-月-日 格式的代码
2016/07/27 Javascript
Bootstrap Table使用方法详解
2016/08/01 Javascript
详细总结Javascript中的焦点管理
2016/09/17 Javascript
React-native桥接Android原生开发详解
2018/01/17 Javascript
微信小程序scroll-view仿拼多多横向滑动滚动条
2020/04/21 Javascript
jQuery zTree插件快速实现目录树
2019/08/16 jQuery
vue 父组件通过v-model接收子组件的值的代码
2019/10/27 Javascript
vue实现轮播图帧率播放
2021/01/26 Vue.js
python调用cmd复制文件代码分享
2013/12/27 Python
Python实现递归遍历文件夹并删除文件
2016/04/18 Python
Python匹配中文的正则表达式
2016/05/11 Python
Python实现判断给定列表是否有重复元素的方法
2018/04/11 Python
python pyinstaller 加载ui路径方法
2019/06/10 Python
Python3.6+selenium2.53.6自动化测试_读取excel文件的方法
2019/09/06 Python
python3.7环境下安装Anaconda的教程图解
2019/09/10 Python
Python数据持久化存储实现方法分析
2019/12/21 Python
Python实现获取当前目录下文件名代码详解
2020/03/10 Python
Python环境管理virtualenv&virtualenvwrapper的配置详解
2020/07/01 Python
Python爬虫之Selenium多窗口切换的实现
2020/12/04 Python
你懂得怎么写自荐信吗?
2013/12/27 职场文书
车辆安全检查制度
2014/01/12 职场文书
企业文明单位申报材料
2014/05/16 职场文书
政府会议通知范文
2015/04/15 职场文书
2015年民兵整组工作总结
2015/07/24 职场文书
消防安全主题班会
2015/08/12 职场文书
阿里云服务器部署mongodb的详细过程
2021/09/04 MongoDB