How to Extract Color Palettes from Images with Python’s Haishoku Library
This guide shows how to use Python’s Haishoku library together with PIL or OpenCV to extract dominant colors and full palettes from any image, then apply the results to improve data‑visualization aesthetics.
When creating data visualizations in Python, choosing an appealing color scheme is crucial; you can derive palettes directly from images using the Haishoku library.
Install Haishoku
pip install haishokuSave the target image locally (e.g., test.png) and load it:
from haishoku.haishoku import Haishoku
image = 'test.png'
haishoku = Haishoku.loadHaishoku(image)Retrieve the dominant color and the full palette:
print(haishoku.dominant) # (R, G, B) tuple
import pprint
pprint.pprint(haishoku.palette) # list of up to 8 (R, G, B) tuplesVisualize the results:
Haishoku.showDominant(image) # opens a preview of the dominant color
Haishoku.showPalette(image) # opens a preview of the paletteSince version 1.1.4, Haishoku can also load images directly from URLs:
imagepath = 'https://img-blog.csdnimg.cn/20190222215216318.png'
haishoku = Haishoku.loadHaishoku(imagepath)Applying the Palette to Visualizations
After extracting a palette (e.g., from the Google logo), you can use the colors in Matplotlib, Seaborn, Plotly, etc., to create charts that match the source image’s aesthetic.
These steps demonstrate that extracting and reusing image‑based color schemes in Python requires only a few lines of code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
