Create colormaps from images in three lines of code!
ImageConverter
class converts images to arrays of RGB values.generate_cmap
creates a matplotlib ListedColormap.from img2cmap import ImageConverter
# Can be a local file or URL
converter = ImageConverter("tests/images/south_beach_sunset.jpg")
cmap = converter.generate_cmap(n_colors=5, palette_name="south_beach_sunset", random_state=42)
Plot an image and a colorbar side by side.
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
fig, ax = plt.subplots(figsize=(7, 5))
ax.axis("off")
img = plt.imread("tests/images/south_beach_sunset.jpg")
im = ax.imshow(img, cmap=cmap)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="10%", pad=0.05)
cb = fig.colorbar(im, cax=cax, orientation="vertical", label=cmap.name)
cb.set_ticks([])
Now, use the colormap in your plots!
import matplotlib.pyplot as plt
colors = colormap.colors
with plt.style.context("dark_background"):
for i, color in enumerate(colors):
plt.plot(range(10), [_+i+1 for _ in range(10)], color=color, linewidth=4)
pip install img2cmap
You can also install the in-development version with:
pip install https://github.com/arvkevi/img2cmap/archive/main.zip
https://img2cmap.readthedocs.io/
docs | |
---|---|
tests | |
package |
To run all the tests run:
tox
Note, to combine the coverage data from all the tox environments run:
Windows | set PYTEST_ADDOPTS=--cov-append tox |
---|---|
Other | PYTEST_ADDOPTS=--cov-append tox |