查看原文
其他

Python|R可视化|09-提取图片颜色绘图(五-颜色使用完结篇)

pythonic生物人 pythonic生物人 2022-09-10

"pythonic生物人"的第32篇分享


原创不易,点个“赞“或"在看"鼓励下呗

摘要

本文是继前面四篇python可视化颜色使用的完结篇介绍如何提取图片中的颜色绘图。

目录

  • Python提取图片颜色

1、颜色提取代码

2、效果展示一

3、效果展示二

  • R提取图片颜色

1、rPlotter包安装

2、颜色提取



正文开始啦


  • Python提取图片颜色

1、颜色提取代码

import cv2import numpy as npfrom PIL import Imageimport 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色号(以下每个list为一个色号,可直接在绘图时使用):

[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]
上面20种颜色绘制一个条形图:


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]

这些颜色的条形图展示:

  • R提取图片颜色

使用rPlotter包,传送门:https://github.com/woobe/rPlotter


1、包安装

## CRAN Packagesinstall.packages(c("ggplot2", "stringr", "reshape2", "dichromat"))
## EBImagesource("http://bioconductor.org/biocLite.R")biocLite("EBImage")
## Packages on GitHublibrary(devtools)install_github("ramnathv/rblocks")
## And finally ...install_github("woobe/rPlotter")


2、颜色提取
  • 提取R语言logo颜色:https://developer.r-project.org/Logo/Rlogo-1.png

## Using the R Logo,以上图片pal_r <- extract_colours("http://developer.r-project.org/Logo/Rlogo-1.png")par(mfrow = c(1,2))pie(rep(1, 5), col = pal_r, main = "Palette based on R Logo")hist(Nile, breaks = 5, col = pal_r, main = "Palette based on R Logo")


  • 其它电影海报颜色效果图:



R感兴趣的小伙伴可以自己试验(PS,好久不用R了)。

同系列文章

Python可视化|matplotlib05-内置单颜色(一)

Python可视化|matplotlib06-外部单颜色(二)

Python可视化|matplotlib07-自带颜色条Colormap(三)

Python可视化|08-Palettable库中颜色条Colormap(四)



原创不易"点赞"、"在看"鼓励下呗


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存