Python 可視化調(diào)色盤繪制
在示例照片當(dāng)中有著各種各樣的顏色,我們將通過Python
中的可視化模塊以及opencv
模塊來識別出圖片當(dāng)中所有的顏色要素,并且將其添加到可視化圖表的配色當(dāng)中
導(dǎo)入模塊并加載圖片
那么按照慣例,第一步一般都是導(dǎo)入模塊,可視化用到的模塊是matplotlib
模塊,我們將圖片中的顏色抽取出來之后會保存在顏色映射表中,所以要使用到colormap
模塊,同樣也需要導(dǎo)入進(jìn)來
import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.patches as patches import matplotlib.image as mpimg from PIL import Image from matplotlib.offsetbox import OffsetImage, AnnotationBbox import cv2 import extcolors from colormap import rgb2hex
然后我們先來加載一下圖片,代碼如下:
input_name = 'test_1.png' img = plt.imread(input_name) plt.imshow(img) plt.axis('off') plt.show()
output:
提取顏色并整合成表格
我們調(diào)用的是extcolors
模塊來從圖片中提取顏色,輸出的結(jié)果是RGB
形式呈現(xiàn)出來的顏色,代碼如下
colors_x = extcolors.extract_from_path(img_url, tolerance=12, limit = 12) colors_x
output:
([((3, 107, 144), 180316),
((17, 129, 140), 139930),
((89, 126, 118), 134080),
((125, 148, 154), 20636),
((63, 112, 126), 18728),
((207, 220, 226), 11037),
((255, 255, 255), 7496),
((28, 80, 117), 4972),
((166, 191, 198), 4327),
((60, 150, 140), 4197),
((90, 94, 59), 3313),
((56, 66, 39), 1669)],
538200)
我們將上述的結(jié)果整合成一個DataFrame
數(shù)據(jù)集,代碼如下:
def color_to_df(input_color): colors_pre_list = str(input_color).replace('([(', '').split(', (')[0:-1] df_rgb = [i.split('), ')[0] + ')' for i in colors_pre_list] df_percent = [i.split('), ')[1].replace(')', '') for i in colors_pre_list] # 將RGB轉(zhuǎn)換成十六進(jìn)制的顏色 df_color_up = [rgb2hex(int(i.split(", ")[0].replace("(", "")), int(i.split(", ")[1]), int(i.split(", ")[2].replace(")", ""))) for i in df_rgb] df = pd.DataFrame(zip(df_color_up, df_percent), columns=['c_code', 'occurence']) return df
我們嘗試調(diào)用上面我們自定義的函數(shù),輸出的結(jié)果至DataFrame
數(shù)據(jù)集當(dāng)中
df_color = color_to_df(colors_x) df_color
output:
繪制圖表
接下來便是繪制圖表的階段了,用到的是matplotlib
模塊,代碼如下:
fig, ax = plt.subplots(figsize=(90,90),dpi=10) wedges, text = ax.pie(list_precent, labels= text_c, labeldistance= 1.05, colors = list_color, textprops={'fontsize': 120, 'color':'black'} ) plt.setp(wedges, width=0.3) ax.set_aspect("equal") fig.set_facecolor('white') plt.show()
output:
從出來的餅圖中顯示了每種不同顏色的占比,我們更進(jìn)一步將原圖放置在圓環(huán)當(dāng)中,
imagebox = OffsetImage(img, zoom=2.3) ab = AnnotationBbox(imagebox, (0, 0)) ax1.add_artist(ab)
output:
最后制作一張調(diào)色盤,將原圖中的各種不同顏色都羅列開來,代碼如下:
## 調(diào)色盤 x_posi, y_posi, y_posi2 = 160, -170, -170 for c in list_color: if list_color.index(c) <= 5: y_posi += 180 rect = patches.Rectangle((x_posi, y_posi), 360, 160, facecolor = c) ax2.add_patch(rect) ax2.text(x = x_posi+400, y = y_posi+100, s = c, fontdict={'fontsize': 190}) else: y_posi2 += 180 rect = patches.Rectangle((x_posi + 1000, y_posi2), 360, 160, facecolor = c) ax2.add_artist(rect) ax2.text(x = x_posi+1400, y = y_posi2+100, s = c, fontdict={'fontsize': 190}) ax2.axis('off') fig.set_facecolor('white') plt.imshow(bg) plt.tight_layout()
output:
實(shí)戰(zhàn)環(huán)節(jié)
這一塊兒是實(shí)戰(zhàn)環(huán)節(jié),我們將上述所有的代碼封裝成一個完整的函數(shù):
def exact_color(input_image, resize, tolerance, zoom): output_width = resize img = Image.open(input_image) if img.size[0] >= resize: wpercent = (output_width/float(img.size[0])) hsize = int((float(img.size[1])*float(wpercent))) img = img.resize((output_width,hsize), Image.ANTIALIAS) resize_name = 'resize_'+ input_image img.save(resize_name) else: resize_name = input_image fig.set_facecolor('white') ax2.axis('off') bg = plt.imread('bg.png') plt.imshow(bg) plt.tight_layout() return plt.show() exact_color('test_2.png', 900, 12, 2.5)
output:
到此這篇關(guān)于Python 可視化調(diào)色盤繪制的文章就介紹到這了,更多相關(guān)Python 可視化 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中re.findAll()、re.sub()、set()的使用
本文主要介紹了Python中re.findAll()、re.sub()、set()的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Python中讀取文件名中的數(shù)字的實(shí)例詳解
在本篇文章里小編給大家整理了一篇關(guān)于Python中讀取文件名中的數(shù)字的實(shí)例詳解內(nèi)容,有興趣的朋友們可以參考下。2020-12-12使用 Python 的 pprint庫格式化和輸出列表和字典的方法
pprint是"pretty-print"的縮寫,使用 Python 的標(biāo)準(zhǔn)庫 pprint 模塊,以干凈的格式輸出和顯示列表和字典等對象,這篇文章主要介紹了如何使用 Python 的 pprint庫格式化和輸出列表和字典,需要的朋友可以參考下2023-05-05Python的基礎(chǔ)語法和輸入輸出函數(shù)你都了解嗎
這篇文章主要為大家詳細(xì)介紹了Python的基礎(chǔ)語法和輸入輸出函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02Django在pycharm下修改默認(rèn)啟動端口的方法
今天小編就為大家分享一篇Django在pycharm下修改默認(rèn)啟動端口的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07pyhton列表轉(zhuǎn)換為數(shù)組的實(shí)例
下面小編就為大家分享一篇pyhton列表轉(zhuǎn)換為數(shù)組的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04python中把元組轉(zhuǎn)換為namedtuple方法
在本篇文章里小編給大家整理的是一篇關(guān)于python中把元組轉(zhuǎn)換為namedtuple方法,有興趣的朋友們可以參考下。2020-12-12Python使用open函數(shù)的buffering設(shè)置文件緩沖方式
這篇文章主要介紹了Python使用open函數(shù)的buffering設(shè)置文件緩沖方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02Python3.5實(shí)現(xiàn)的三級菜單功能示例
這篇文章主要介紹了Python3.5實(shí)現(xiàn)的三級菜單功能,涉及Python針對json格式數(shù)據(jù)的讀取、遍歷、查找、判斷等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03