current position:Home>A python artifact handles automatic chart color matching

A python artifact handles automatic chart color matching

2022-01-31 10:49:58 Open source outpost

The following article comes from quick learning Python , Author Zhu Xiaowu

We're using Python When visualizing data , There are a large number of high-quality libraries that can be used , such as :MatplotlibseabornPlotlyBokehggplot wait . But the chart is not good , Color matching accounts for half . Without good aesthetics , Things that are easy to make are hot eyes ……

So I want to do a good job in data visualization , You have to have the right color scheme . In addition to the cases of reference color matching websites , You can also customize a set of color schemes .

How to customize it ?

I have an idea , The beauty of color matching needs to be cultivated , But at the beginning, you can find inspiration in excellent works , Like classic movies 、 posters 、 Landscape 、Logo wait , These are great references .

The colors of natural scenery are often amazing , Let's take a landscape map as an example . The picture below shows a sunset at sea , Through some operations, a set of color scheme is extracted ( See figure right ).

that , We use it Python Can we do it ?

The answer, of course, is yes , After all Python Except you can't have children , You can do anything !

Extract the color in the picture

stay Python Operate on the picture in , The two most commonly used modules are PIL and opencv 了 . So at first my plan was , use Python Library open picture , Then traverse the pixel color , Finally, sort according to the color scale , You can get the color scheme of the picture .

The result is half done , I found myself ignoring one thing . Everybody knows ,Python It's an elegant language , Simple grammar , Powerful features . At the same time, it also has an extremely rich third-party library , Almost all of these libraries can be found in github perhaps pypi Source code found on .

So I searched , There are indeed relevant libraries that can extract color matching from pictures , Then we don't have to build the wheel again .

This module is ——Haishoku, It can be used to obtain the main color and main color scheme from the image .

Its GitHub The website is :github.com/LanceGin/ha…

The specific use , Install first

pip install haishoku
 Copy code 

The sunset on the sea mentioned above , Save locally and name it test.png.

from haishoku.haishoku import Haishoku
image = 'test.png'
haishoku = Haishoku.loadHaishoku(image)
 Copy code 

The import module , Running the code will return a Haishoku example , You can use instance properties haishoku.dominant and haishoku.palette, So as to directly obtain the corresponding main color and color matching scheme .

Main tone

First , How to get the main color of the picture ?

print(haishoku.dominant)
 Copy code 

This returns a structure of (R, G, B) tuples , Is the main color of the picture .

Run the following line of code

Haishoku.showDominant(image)
 Copy code 

A temporary file will be opened , The color used to preview the main tone .

 Main tone ( Most colors )

The color scheme

# Get color scheme 
pprint.pprint(haishoku.palette)
 Copy code 

Returns a structure of :[(R, G, B), (R, G, B), …] Maximum length is 8 Array of .

It's used here pprint modular , For this multi-layer nested tuple , It can be printed beautifully .

Run the following line of code

Haishoku.showPalette(image)
 Copy code 

A temporary file will be opened , Color scheme used to preview pictures .( Will not be saved locally )

 The color scheme

That's it , Just a few lines of code to extract the color scheme in the picture , Is it simple .

in addition ,Haishoku Library from v1.1.4 After version , Support from the url Load images directly in .

imagepath = 'https://img-blog.csdnimg.cn/20190222215216318.png'
    
haishoku = Haishoku.loadHaishoku(imagepath)
 Copy code 

Color scheme and visualization

Through the previous operation , We extracted the right color , Then let's fight it .

Classic movies 、 posters 、 Landscape 、Logo Are excellent reference objects .

So this time , I chose Google Of Logo, And extract its color scheme .

imagepath = 'google.png'

haishoku = Haishoku.loadHaishoku(imagepath)

pprint.pprint(haishoku.palette)

Haishoku.showPalette(imagepath)
 Copy code 

that , This color scheme is applied to data visualization , What will happen ??

This time with what I just got Google Color matching ,Python Try drawing a ring chart

It feels good , I want to collect this color scheme . If you think this article is good , Remember to give a key three times !

Open source outpost Everyday sharing is hot 、 Interesting and practical open source projects . Participate in maintenance 10 ten thousand + Star Open source technology repository for , Include :Python、Java、C/C++、Go、JS、CSS、Node.js、PHP、.NET etc. .

copyright notice
author[Open source outpost],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311049571177.html

Random recommended