python判断、获取一张图片主色调的2个实例

yipeiwu_com5年前Python基础

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()

 

相关文章

Python3实现从文件中读取指定行的方法

本文实例讲述了Python3实现从文件中读取指定行的方法。分享给大家供大家参考。具体实现方法如下: # Python的标准库linecache模块非常适合这个任务 import li...

python中WSGI是什么,Python应用WSGI详解

为了让大家更好的对python中WSGI有更好的理解,我们先从最简单的认识WSGI着手,然后介绍一下WSGI几个经常使用到的接口,了解基本的用法和功能,最后,我们通过实例了解一下WSGI...

python调用外部程序的实操步骤

python调用外部程序的实操步骤

在python的使用中,有时也不得不调用一下外部程序,那么如何调用外部程序: 首先,我们要启动python软件,使用的是python2.7的版本,具体如图: 在外部调用中主要要用到一...

Django之使用内置函数和celery发邮件的方法示例

Django之使用内置函数和celery发邮件的方法示例

邮箱配置 开启stmp服务 以163邮箱为例,点击设置里面的stmp 开启客户端授权密码 如上所示,因为我已经开启了,所以出现的是以上页面。 这样,邮箱的准备就已经完成了。 使用Dj...

python中dict字典的查询键值对 遍历 排序 创建 访问 更新 删除基础操作方法

字典是另一种可变容器模型,且可存储任意类型对象。 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ; 字典值可...