Python可视化matplotlib10-提取图片颜色(五-完结篇)

    技术2022-07-10  96

    本文是继前面四篇python可视化颜色使用的完结篇,介绍如何提取图片中的颜色绘图;如果你不想使用前人设定好的色号或者colormap,想自己从好看的图片中提取颜色,请往下看。

    目录

    欢迎随缘关注@pythonic生物人

    1、颜色提取代码

    2、效果展示一​

    3、效果展示二 


    1、颜色提取代码

    #部分参考了别的博主,不过链接忘记了 import cv2 import numpy as np from PIL import Image import matplotlib.pyplot as plt img_path = 'mh.jpg' image = Image.open(img_path) # 要提取的主要颜色数量 num_colors = 20 small_image = image.resize((80, 80)) result = small_image.convert('P', palette=Image.ADAPTIVE, colors=num_colors) result = result.convert('RGB') main_colors = result.getcolors() col_extract = [] # 显示提取的主要颜色 for count, col in main_colors: #print([col[i]/255 for i in range(3)])#RGB转RGBA,可输出RGBA色号 col_extract.append([col[i]/255 for i in range(3)]) #使用提取的颜色绘制条形图 plt.figure(dpi=150) plt.bar(range(len(col_extract)),np.ones(len(col_extract)),color=(col_extract)) plt.xticks(range(len(col_extract)), (range(len(col_extract)))) plt.show

    2、效果展示一

     比如提取下图这张电影海报上面的颜色来绘图是什么样子的了?PS,电影名为《生活多美好》,强烈建议那些天天想仗剑走天涯的人去看,电影的目的就是告诉我们,眼前也有好风景,不必总得去远方,不扯远了。

     排名前20的RGBA色号:

    [0.9921568627450981, 0.8980392156862745, 0.788235294117647] [0.9607843137254902, 0.12941176470588237, 0.11764705882352941] [0.8784313725490196, 0.8352941176470589, 0.7019607843137254] [0.8352941176470589, 0.6470588235294118, 0.5333333333333333] [0.792156862745098, 0.2235294117647059, 0.19607843137254902] [0.7215686274509804, 0.5529411764705883, 0.4549019607843137] [0.6588235294117647, 0.45098039215686275, 0.37254901960784315] [0.5176470588235295, 0.6549019607843137, 0.49019607843137253] [0.47058823529411764, 0.3803921568627451, 0.32941176470588235] [0.9803921568627451, 0.8549019607843137, 0.5333333333333333] [0.9921568627450981, 0.9176470588235294, 0.8196078431372549] [0.9803921568627451, 0.8705882352941177, 0.7568627450980392] [0.9725490196078431, 0.9019607843137255, 0.6666666666666666] [0.9411764705882353, 0.7607843137254902, 0.6431372549019608] [0.8235294117647058, 0.7450980392156863, 0.6588235294117647] [0.6549019607843137, 0.7058823529411765, 0.5843137254901961] [0.4823529411764706, 0.5254901960784314, 0.45098039215686275] [0.3568627450980392, 0.27450980392156865, 0.2627450980392157] [0.3176470588235294, 0.5254901960784314, 0.3568627450980392] [0.26666666666666666, 0.17254901960784313, 0.20392156862745098]

    这些颜色的条形图展示:

    3、效果展示二 

    排名前20RGBA色号 

    [1.0, 0.6235294117647059, 0.4627450980392157] [0.9490196078431372, 0.7803921568627451, 0.611764705882353] [0.9450980392156862, 0.7019607843137254, 0.8235294117647058] [0.9215686274509803, 0.6627450980392157, 0.48627450980392156] [0.9176470588235294, 0.6235294117647059, 0.4470588235294118] [0.2196078431372549, 0.6392156862745098, 0.2823529411764706] [0.792156862745098, 0.5058823529411764, 0.34901960784313724] [0.36470588235294116, 0.7803921568627451, 0.3568627450980392] [0.2901960784313726, 0.6862745098039216, 0.27450980392156865] [0.22745098039215686, 0.6235294117647059, 0.2901960784313726] [0.2196078431372549, 0.6470588235294118, 0.2784313725490196] [0.6509803921568628, 0.17254901960784313, 0.1607843137254902] [0.6470588235294118, 0.611764705882353, 0.592156862745098] [0.6, 0.807843137254902, 0.8196078431372549] [0.5882352941176471, 0.7529411764705882, 0.7215686274509804] [0.5843137254901961, 0.8627450980392157, 0.6] [0.9176470588235294, 0.8980392156862745, 0.7294117647058823] [0.2901960784313726, 0.43137254901960786, 0.44313725490196076] [0.13725490196078433, 0.6196078431372549, 0.3411764705882353] [0.07450980392156863, 0.13333333333333333, 0.2] 这些颜色的条形图展示:


    欢迎随缘关注@pythonic生物人

     

     

     

    Processed: 0.027, SQL: 9