最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python判斷、獲取一張圖片主色調(diào)的2個(gè)實(shí)例

 更新時(shí)間:2014年04月10日 14:51:19   作者:  
一幅圖片,想通過程序判斷獲得其主要色調(diào),應(yīng)該怎么樣處理?本文通過python實(shí)現(xiàn)判斷、獲取一張圖片的主色調(diào)方法,需要的朋友可以參考下

python判斷圖片主色調(diào),單個(gè)顏色:

復(fù)制代碼 代碼如下:

#!/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è)顏色:

復(fù)制代碼 代碼如下:

#!/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()

 

相關(guān)文章

  • 在Python中定義一個(gè)常量的方法

    在Python中定義一個(gè)常量的方法

    今天小編就為大家分享一篇在Python中定義一個(gè)常量的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • 在pycharm中配置Anaconda以及pip源配置詳解

    在pycharm中配置Anaconda以及pip源配置詳解

    這篇文章主要介紹了在pycharm中配置Anaconda以及pip源配置詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • python游戲?qū)崙?zhàn)項(xiàng)目之智能五子棋簡易版

    python游戲?qū)崙?zhàn)項(xiàng)目之智能五子棋簡易版

    利用Python實(shí)現(xiàn)智能五子棋,實(shí)現(xiàn)之后發(fā)現(xiàn)我玩不贏它!本篇為你帶來用python編寫的五子棋小游戲,文中給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值
    2021-09-09
  • pytorch程序異常后刪除占用的顯存操作

    pytorch程序異常后刪除占用的顯存操作

    今天小編就為大家分享一篇pytorch程序異常后刪除占用的顯存操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • python之關(guān)于數(shù)組和列表的區(qū)別及說明

    python之關(guān)于數(shù)組和列表的區(qū)別及說明

    這篇文章主要介紹了python之關(guān)于數(shù)組和列表的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 構(gòu)建可視化?web的?Python?神器streamlit

    構(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)方法

    這篇文章主要介紹了python實(shí)現(xiàn)帶聲音的摩斯碼翻譯實(shí)現(xiàn)方法,涉及pygame模塊操作及摩斯碼實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-05-05
  • python實(shí)現(xiàn)按首字母分類查找功能

    python實(shí)現(xiàn)按首字母分類查找功能

    這篇文章主要介紹了python實(shí)現(xiàn)按首字母分類查找功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • Python使用matplotlib實(shí)現(xiàn)繪制自定義圖形功能示例

    Python使用matplotlib實(shí)現(xiàn)繪制自定義圖形功能示例

    這篇文章主要介紹了Python使用matplotlib實(shí)現(xiàn)繪制自定義圖形功能,結(jié)合實(shí)例形式分析了Python基于matplotlib模塊實(shí)現(xiàn)自定義圖形繪制相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • 對(duì)python中數(shù)據(jù)集劃分函數(shù)StratifiedShuffleSplit的使用詳解

    對(duì)python中數(shù)據(jù)集劃分函數(shù)StratifiedShuffleSplit的使用詳解

    今天小編就為大家分享一篇對(duì)python中數(shù)據(jù)集劃分函數(shù)StratifiedShuffleSplit的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12

最新評(píng)論

星座| 安溪县| 崇明县| 衡阳县| 麻城市| 新郑市| 安国市| 资阳市| 道孚县| 历史| 曲靖市| 正阳县| 精河县| 和硕县| 潼关县| 筠连县| 大兴区| 凌云县| 灵川县| 汝州市| 元谋县| 稻城县| 新化县| 贵港市| 内黄县| 平舆县| 永城市| 泸西县| 新龙县| 红桥区| 玉山县| 永平县| 芜湖市| 邓州市| 佛坪县| 富阳市| 行唐县| 湟中县| 互助| 仙桃市| 邯郸市|