colormap-tool
A Colormap Tool package to convert cmps between cv and mpl.
- Github repository: https://github.com/MeridianInnovation/colormap-tool/
- Documentation https://MeridianInnovation.github.io/colormap-tool/
Overview
This package can let users use cv's built-in colormap in matplotlib, or use matplotlib's colormap in cv.
Features
- Convert colormaps between matplotlib and OpenCV formats
- Access colormaps from matplotlib and OpenCVs through a common interface
- Convert between numpy arrays, matplotlib Colormap objects, and OpenCV constants
- Register external colormaps with matplotlib
Installation
To install the project, run the following command:
python -m pip install colormap-tool
Usage
1. Basic Import
import cv2
import matplotlib.pyplot as plt
import numpy as np
import colormap_tool
2. Core Recipes
This library provides simple, one-line solutions for common colormap conversion tasks.
Recipe 1: Use a Matplotlib Colormap in OpenCV
To apply a Matplotlib colormap (e.g., viridis) to an image using cv2.applyColorMap, use get_cv_colormaps.
This function automatically handles the conversion to the BGR format required by OpenCV.
# Get the 'viridis' colormap in a format suitable for OpenCV
lut = colormap_tool.get_cv_colormaps("mpl.viridis")
# Apply it to a grayscale image
gray_img = np.random.randint(0, 255, (100, 100), dtype=np.uint8)
colored_img = cv2.applyColorMap(gray_img, lut)
# Display the result
cv2.imshow("Matplotlib's Viridis in OpenCV", colored_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Recipe 2: Use an OpenCV Colormap in Matplotlib
To use an OpenCV colormap (e.g., jet) in a Matplotlib plot, use get_mpl_colormaps.
This function returns a matplotlib.colors.Colormap object that matplotlib understands.
# Get the OpenCV 'jet' colormap as a Matplotlib Colormap object
cmap = colormap_tool.get_mpl_colormaps("cv.jet")
# Use it in a plot
data = np.random.rand(20, 20)
plt.imshow(data, cmap=cmap)
plt.title("OpenCV's Jet in Matplotlib")
plt.colorbar()
plt.show()
Recipe 3: Get Raw Colormap Data (RGB)
If you need the raw RGB color data for a colormap, use get_colormaps. This is useful for custom processing, analysis, or creating custom visualizations.
You can also specify the number of entries (n) to resample the colormap.
# Get the 'viridis' colormap as a 256-entry RGB array
rgb_lut = colormap_tool.get_colormaps("mpl.viridis", n=256)
# rgb_lut is a (256, 3) numpy array with dtype=uint8
print(rgb_lut.shape)
print(rgb_lut.dtype)
3. Advanced Usage
Registering All Colormaps with Matplotlib
For maximum convenience, you can register all available colormaps with Matplotlib at the start of your script. This allows you to use them by name in any Matplotlib function.
# Register all colormaps
colormap_tool.register_all_cmps2mpl()
# Now, you can use OpenCV colormaps directly by name
data = np.random.rand(20, 20)
plt.imshow(data, cmap="cv.jet")
plt.title("Using a Registered OpenCV Colormap")
plt.show()
Custom LUT Resampling
The resample_lut function can resize any custom LUT to a desired length while preserving its format.
# Create a simple 2-color LUT (black to white)
my_lut = np.array([[0, 0, 0], [255, 255, 255]], dtype=np.uint8)
# Resample it to 10 entries
resampled_lut = colormap_tool.resample_lut(my_lut, 10)
print(resampled_lut.shape) # Output: (10, 3)
Converting a Custom RGB Array to a Matplotlib Colormap
If you have your own colormap data as an RGB array, you can convert it into a Matplotlib Colormap object.
# Create a custom gradient from blue to yellow
custom_rgb = np.zeros((256, 3), dtype=np.uint8)
custom_rgb[:, 0] = np.linspace(0, 255, 256) # R
custom_rgb[:, 1] = np.linspace(0, 255, 256) # G
custom_rgb[:, 2] = np.linspace(255, 0, 256) # B
# Convert it to a Matplotlib Colormap object
custom_cmap = colormap_tool.uint8_rgb_arr2mpl_cmp(custom_rgb, name="blue_yellow")
# Use it in a plot
plt.imshow(data, cmap=custom_cmap)
plt.title("Custom Blue-Yellow Colormap")
plt.show()
License
This project is licensed under the MIT license license.
Contributing
Please follow the Contributing Guide to contribute to this project.
Contact
For support or inquiries, please contact:
- Email: info@meridianinno.com
Metadata
Release files for colormap-tool 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| colormap_tool-1.1.0.tar.gz | 125.3 kB | Without provenance | Details for colormap_tool-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| colormap_tool-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for colormap_tool-1.1.0-py3-none-any.whl |
Total release size: 249.0 kB
Release files / colormap_tool-1.1.0.tar.gz
| Download URL | colormap_tool-1.1.0.tar.gz |
|---|---|
| Size | 125.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8d415c6e3982834b0e625a70824f855d937fabea735a953a33252e43b1f8312c
|
|
BLAKE2b-256 checksum How to use checksums |
7b86d13add79b3ccdd8c0b41a3b84ada5f1fe91d0dc97fea7b2ee564360dcdd8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.6.16
|
Release files / colormap_tool-1.1.0-py3-none-any.whl
| Download URL | colormap_tool-1.1.0-py3-none-any.whl |
|---|---|
| Size | 123.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
840e21d1c7f86cd57cc975d8d925d3203c2060c6f197ae622193e6c18b44a01e
|
|
BLAKE2b-256 checksum How to use checksums |
de23541607c4807ce8a442e35a27a8a116beda2fac15644eed5035b19671e7ca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.6.16
|
Security for colormap-tool 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.