Tutorial¶
Colorspacious is a Python library that lets you easily convert between colorspaces like sRGB, XYZ, CIEL*a*b*, CIECAM02, CAM02-UCS, etc. If you have no idea what these are or what each is good for, and reading this list makes you feel like you’re swimming in alphabet soup, then this video provides a basic orientation and some examples. (The overview of color theory starts at ~3:35.)
Now let’s see some cut-and-pasteable examples of what
colorspacious
is good for. We’ll start by loading up some
utility modules for numerics and plotting that we’ll use later:
In [1]: import numpy as np
In [2]: import matplotlib
In [3]: import matplotlib.pyplot as plt
Now we need to import colorspacious
. The main function we’ll
use is cspace_convert()
:
In [4]: from colorspacious import cspace_convert
This allows us to convert between many color spaces. For example, suppose we want to know how the color with coordinates (128, 128, 128) in sRGB space (represented with values between 0 and 255) maps to XYZ space (represented with values between 0 and 100):
In [5]: cspace_convert([128, 128, 128], "sRGB255", "XYZ100")
Out[5]: array([20.51692894, 21.58512253, 23.506738 ])
Colorspacious knows about a wide variety of colorspaces, and you can convert between any of them by
naming them in a call to cspace_convert()
.
We can also conveniently work on whole images. Let’s load one up as an example.
# if you want this file, try:
# hopper_sRGB = plt.imread(matplotlib.cbook.get_sample_data("grace_hopper.png"))
In [6]: hopper_sRGB = plt.imread("grace_hopper.png")
What have we got here?
In [7]: hopper_sRGB.shape
Out[7]: (600, 512, 3)
In [8]: hopper_sRGB[:2, :2, :]