The original link: mp.weixin.qq.com/s/lwvZ1Syhi… Author: Zhu Xiaowu

There are a number of high-quality libraries available for data visualization in Python, such as Matplotlib, Seaborn, Plotly, Bokeh, GGPlot, and more. But half of the chart is color matching. If there is no good aesthetic sense, it is easy to make things hot eyes…

So if you want to do a good job of data visualization, you need to have the right color scheme. In addition to reference the case of color matching website, you can also customize a set of color matching scheme.

How do you customize it?

I do have an idea that the beauty of color matching needs to be cultivated, but at the beginning you can look for inspiration from excellent works, such as classic movies, posters, landscapes, logos, etc. These are excellent references.

The colors of natural landscapes are often amazing. Let’s take landscape pictures for example. Below is an image of a sunset over the sea, with some manipulation to extract a color scheme (see image right).

So, can we do that in Python?

The answer, of course, is yes, after all Python can do everything except give birth!

Extract the color scheme from the picture

PIL and OpencV are the two most commonly used modules for manipulating images in Python. So my initial solution was to open the image using the Python library, iterate through the pixel colors, and finally sort by color scale to get the color scheme for the image.

In the middle of it, I realized I had overlooked one thing. As you all know, Python is an elegant language with concise syntax and powerful features. It also has a wealth of third-party libraries, almost all of which can be found on Github or Pypi.

So I googled it, and there was a library for extracting color schemes from images, so we didn’t have to reinvent the wheel.

This module, called Haishoku, can be used to get the main hue and the main color scheme from an image.

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

Specific usage, or install first

pip install haishoku
Copy the code

Save the above sunset image locally and name it test.png.

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

Importing the module, running the code will return an instance of Haishoku, which you can retrieve directly from the instance properties haishoku.dominant and haishoku.Palette.

The mass-tone attune

First of all, how do you get the main color of an image?

print(haishoku.dominant)
Copy the code

This returns a tuple of structure (R, G, B), which is the main hue of the image.

Run the following line

Haishoku.showDominant(image)
Copy the code

A temporary file is opened to preview the main colors.

Primary (most colors)

The color scheme

Pprint (haishoku.palette)Copy the code

Return a structure of [(R, G, B), (R, G, B)… An array of maximum length 8.

Here we use the pprint module, which makes it nice to print this multilayer nested tuple.

Run the following line

Haishoku.showPalette(image)
Copy the code

A temporary file is opened to preview the image color scheme. (Not saved locally)

The color scheme

That’s how easy it is to extract the color scheme from an image with just a few lines of code.

In addition, the Haishoku library supports loading images directly from urls since version 1.1.4.

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

Color scheme and visualization

Through the previous operation, we have extracted the appropriate color match, so let’s practice it.

Classic movies, posters, landscapes, and logos are excellent references.

So this time, I chose the Google Logo and extracted its color scheme.

imagepath = 'google.png'

haishoku = Haishoku.loadHaishoku(imagepath)

pprint.pprint(haishoku.palette)

Haishoku.showPalette(imagepath)
Copy the code

So,, this color scheme applied to data visualization, what will it be?

Try drawing a pie chart in Python

I feel good about it. I want this color scheme for my collection. If you think this article is good, remember to give a key three link!

This article’s code and file packaging: wwe.lanzoui.com/iepThvcl9zi

In fact, at the beginning, their own custom extraction picture color is not a bit of use, I also with OpenCV made a color picker.

That is, the Python runtime can simply click to get the color of the current image location.

If you are interested, please click on the lower right corner of the like bar, this text like more than 30👍, update as soon as possible!!


At the end of this article, I will introduce three online documents written by myself:

The first document: PyCharm Chinese Guide 1.0 document

It took me more than two months to sort out the 100 Tips of PyCharm. In order for beginners to be able to use it directly, I spent a lot of time to record hundreds of GIFs and read online documents for those who are interested.

Second document: PyCharm Dark Magic Guide 1.0 document

The system includes a variety of cold Python knowledge, Python Shell diverse gameplay, crazy Python skills, Python super detailed advanced knowledge interpretation, very practical Python development skills.

Third document: The Python Chinese Guide 1.0 document

It takes three months to write a full Chinese tutorial for beginners to get started with Python, with a large number of code examples, so that beginners can have a visual experience of the operation of the code. The tutorials are both in-depth and extensive, and each article is marked with difficulty, whether it is basic or advanced. Python is a rare electronic tutorial in Python Chinese.