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庫(kù)處理圖片的實(shí)例教程
- Python編程中使用Pillow來(lái)處理圖像的基礎(chǔ)教程
- 在Mac OS系統(tǒng)上安裝Python的Pillow庫(kù)的教程
- python判斷圖片寬度和高度后刪除圖片的方法
- Python讀取圖片屬性信息的實(shí)現(xiàn)方法
- python如何在終端里面顯示一張圖片
- python圖片驗(yàn)證碼生成代碼
- Python的Tornado框架實(shí)現(xiàn)圖片上傳及圖片大小修改功能
- Python下載指定頁(yè)面上圖片的方法
- Python 多線程抓取圖片效率對(duì)比
- Python基于pillow判斷圖片完整性的方法
相關(guān)文章
Python3日期與時(shí)間戳轉(zhuǎn)換的幾種方法詳解
我們可以利用內(nèi)置模塊 datetime 獲取當(dāng)前時(shí)間,然后將其轉(zhuǎn)換為對(duì)應(yīng)的時(shí)間戳。這篇文章主要介紹了Python3日期與時(shí)間戳轉(zhuǎn)換的幾種方法,需要的朋友可以參考下2019-06-06Python中解析JSON并同時(shí)進(jìn)行自定義編碼處理實(shí)例
這篇文章主要介紹了Python中解析JSON并同時(shí)進(jìn)行自定義編碼處理實(shí)例,需要的朋友可以參考下2015-02-02python實(shí)現(xiàn)顏色空間轉(zhuǎn)換程序(Tkinter)
這篇文章主要介紹了基于Tkinter利用python實(shí)現(xiàn)顏色空間轉(zhuǎn)換程序,感興趣的小伙伴們可以參考一下2015-12-12python在協(xié)程中增加任務(wù)實(shí)例操作
在本篇文章里小編給大家整理的是一篇關(guān)于python在協(xié)程中增加任務(wù)實(shí)例操作內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-02-02如何利用Python連接MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)數(shù)據(jù)儲(chǔ)存
當(dāng)我們學(xué)習(xí)了mysql數(shù)據(jù)庫(kù)后,我們會(huì)想著該如何將python和mysql結(jié)合起來(lái)運(yùn)用,下面這篇文章主要給大家介紹了關(guān)于如何利用Python連接MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)數(shù)據(jù)儲(chǔ)存的相關(guān)資料,需要的朋友可以參考下2021-11-11