python判斷、獲取一張圖片主色調(diào)的2個(gè)實(shí)例
python判斷圖片主色調(diào),單個(gè)顏色:
#!/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判斷一張圖片的主色調(diào),多個(gè)顏色:
#!/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()
- Linux上安裝Python的PIL和Pillow庫處理圖片的實(shí)例教程
- Python編程中使用Pillow來處理圖像的基礎(chǔ)教程
- 在Mac OS系統(tǒng)上安裝Python的Pillow庫的教程
- python判斷圖片寬度和高度后刪除圖片的方法
- Python讀取圖片屬性信息的實(shí)現(xiàn)方法
- python如何在終端里面顯示一張圖片
- python圖片驗(yàn)證碼生成代碼
- Python的Tornado框架實(shí)現(xiàn)圖片上傳及圖片大小修改功能
- Python下載指定頁面上圖片的方法
- Python 多線程抓取圖片效率對比
- Python基于pillow判斷圖片完整性的方法
相關(guān)文章
python游戲?qū)崙?zhàn)項(xiàng)目之智能五子棋簡易版
利用Python實(shí)現(xiàn)智能五子棋,實(shí)現(xiàn)之后發(fā)現(xiàn)我玩不贏它!本篇為你帶來用python編寫的五子棋小游戲,文中給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值2021-09-09
python之關(guān)于數(shù)組和列表的區(qū)別及說明
這篇文章主要介紹了python之關(guān)于數(shù)組和列表的區(qū)別及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
構(gòu)建可視化?web的?Python?神器streamlit
這篇文章主要介紹了構(gòu)建可視化web的Python神器streamlit,Streamlit是一個(gè)用于機(jī)器學(xué)習(xí)、數(shù)據(jù)可視化的Python框架,它能幾行代碼就構(gòu)建出一個(gè)精美的在線app應(yīng)用2022-06-06
python實(shí)現(xiàn)帶聲音的摩斯碼翻譯實(shí)現(xiàn)方法
這篇文章主要介紹了python實(shí)現(xiàn)帶聲音的摩斯碼翻譯實(shí)現(xiàn)方法,涉及pygame模塊操作及摩斯碼實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-05-05
Python使用matplotlib實(shí)現(xiàn)繪制自定義圖形功能示例
這篇文章主要介紹了Python使用matplotlib實(shí)現(xiàn)繪制自定義圖形功能,結(jié)合實(shí)例形式分析了Python基于matplotlib模塊實(shí)現(xiàn)自定義圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
對python中數(shù)據(jù)集劃分函數(shù)StratifiedShuffleSplit的使用詳解
今天小編就為大家分享一篇對python中數(shù)據(jù)集劃分函數(shù)StratifiedShuffleSplit的使用詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12

