Skip to main content

Create colormaps from images

Project description

Usage

Basic

Create colormaps from images in three lines of code!

First, ImageConverter class converts images to arrays of RGB values.
Then, 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([])
images/colorbar.png

Now, use the colormap in your plots!

import matplotlib.pyplot as plt

colors = cmap.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)
images/img2cmap_demo.png

Advanced

remove_transparency

In an image of the Los Angeles Lakers logo, the background is transparent. These pixels contribute to noise when generating the colors. Setting remove_transparency to True will remove transparent pixels. Here’s a comparison of the colormaps generated by the same image, without and with transparency removed.

Make two ImageConverter objects:

from img2cmap import ImageConverter

image_url = "https://loodibee.com/wp-content/uploads/nba-los-angeles-lakers-logo.png"

# Create two ImageConverters, one with transparency removed and one without
converter_with_transparent = ImageConverter(image_url, remove_transparent=False)
converter_no_transparent = ImageConverter(image_url, remove_transparent=True)

cmap_with_transparent = converter_with_transparent.generate_cmap(
    n_colors=3, palette_name="with_transparent", random_state=42
)
cmap_no_transparent = converter_no_transparent.generate_cmap(
    n_colors=3, palette_name="no_transparent", random_state=42
)

Plot both colormaps with the image:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

for cmap in [cmap_with_transparent, cmap_no_transparent]:
    fig, ax = plt.subplots(figsize=(7, 5))

    ax.axis("off")
    img = converter_no_transparent.image
    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([])
images/lakers_with_transparent.png images/lakers_no_transparent.png

Notice, only after removing the transparent pixels, does the classic purple and gold show in the colormap.

generate_optimal_cmap

You can extract the optimal number of colors from the image using the generate_optimal_cmap method. Under the hood this performs the elbow method <https://en.wikipedia.org/wiki/Elbow_method_(clustering)> to determine the optimal number of clusters based on the sum of the squared distances between each pixel and it’s cluster center.

cmaps, best_n_colors, ssd = converter.generate_optimal_cmap(max_colors=10, random_state=42)

best_cmap = cmaps[best_n_colors]

Installation

pip install img2cmap

You can also install the in-development version with:

pip install https://github.com/arvkevi/img2cmap/archive/main.zip

Documentation

https://img2cmap.readthedocs.io/

Status

Development

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

Changelog

0.0.0 (2022-04-30)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

img2cmap-0.2.0.tar.gz (5.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

img2cmap-0.2.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file img2cmap-0.2.0.tar.gz.

File metadata

  • Download URL: img2cmap-0.2.0.tar.gz
  • Upload date:
  • Size: 5.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.2

File hashes

Hashes for img2cmap-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b0c1f62917aeb2bee538c77ae1eabfb7ed6c5aeb062ffdfa7d04b9bfbcdfc7fa
MD5 75f20c29b4763f7125da3302ba8fe50c
BLAKE2b-256 e7054102f52fe896de127965bdd0cf6b48f55cf1ff560b8c5cc6a56a2cda5ddd

See more details on using hashes here.

File details

Details for the file img2cmap-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: img2cmap-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.2

File hashes

Hashes for img2cmap-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 200c8ff54ba41b1245c91e5d723024f9267298871c08226631a5bf3c0236eeed
MD5 f688eddeeb3bb2d6ae6ef99385f7b136
BLAKE2b-256 00bd6a8e451e898dae7c8ce04f0a85923d994ca8e1ffb8ea3d0e3a9b984f0d04

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page