File: delta_e_matrix.py

package info (click to toggle)
python-colormath 3.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 308 kB
  • sloc: python: 3,657; makefile: 4
file content (39 lines) | stat: -rw-r--r-- 1,297 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
For a massive matrix of colors and color labels you can download
the follow two files

# http://lyst-classifiers.s3.amazonaws.com/color/lab-colors.pk
# http://lyst-classifiers.s3.amazonaws.com/color/lab-matrix.pk

lab-colors is a cPickled list of color names and lab-matrix is a
cPickled (n,3) numpy array LAB values such that row q maps to
index q in the lab color list
"""

import sys
import csv
import bz2

import numpy as np

# Does some sys.path manipulation so we can run examples in-place.
# noinspection PyUnresolvedReferences
import example_config

from colormath.color_diff_matrix import delta_e_cie2000
from colormath.color_objects import LabColor


# load list of 1000 random colors from the XKCD color chart
if sys.version_info >= (3, 0):
    reader = csv.DictReader(bz2.open('lab_matrix.csv.bz2', mode='rt'))
    lab_matrix = np.array([list(map(float, row.values())) for row in reader])
else:
    reader = csv.DictReader(bz2.BZ2File('lab_matrix.csv.bz2'))
    lab_matrix = np.array([map(float, row.values()) for row in reader])

color = LabColor(lab_l=69.34, lab_a=-0.88, lab_b=-52.57)
lab_color_vector = np.array([color.lab_l, color.lab_a, color.lab_b])
delta = delta_e_cie2000(lab_color_vector, lab_matrix)

print('%s is closest to %s' % (color, lab_matrix[np.argmin(delta)]))