Package: color-extraction
Color-extractionColor-extraction is an open-source python module which attributes to each element of an ndarray (RGB image) the most similar color from a palette of predefined colors.Three functions are included, each of which takes an RGB ndarray as input and returns a dict whose keys are the names of each predefined color:FunctionValues of returned dictget_bool_arraysboolean ndarrays (1 per color)get_rgb_arraysRGB ndarrays (1 per color)get_countsinteger counts of pixels (1 per color)Installing>>> pip install color_extractionUsage examplesA predefined set of colors is included in the module with ten colors: red,
orange, yellow, green, cyan, blue, purple, pink, achromatic (gray and black),
and white. This set of colors, which can be modified, is available athttps://github.com/ChrisCocco/ddd_colours/blob/master/color_extraction/color_definitions.json.To get started:>>> import color_extraction
>>> import matplotlib
>>> import matplotlib.pyplot

>>> with matplotlib.cbook.get_sample_data('ada.png') as image_file:
... img = matplotlib.pyplot.imread(image_file)Boolean arraysThe functionget_bool_arraysreturns a dictionary with a boolean ndarray for each color. Each such array has the same horizontal and vertical dimensions as the source image and can be thought of as amaskfor the color in question.>>> dict_bool_arrays = color_extraction.get_bool_arrays(img)

>>> for color in dict_bool_arrays.keys():
    matplotlib.image.imsave(output_path + color + ".png", dict_bool_arrays[color], cmap='gray')Original imagewhiteredorangeyellowgreencyanbluepurplepinkachromaticIt is also possible to use a median filter (3 x 3) in order to reduce the amount of pixels of a given color that are isolated in the array:>>> color_extraction.get_bool_arrays(img, median_filter=True)It is also possible to use your own color definitions saved in a JSON file.>>> color_extraction.get_bool_arrays(img, color_def_path=path_to_your_json_file)RGB arraysThe functionget_rgb_arraysreturns a dictionary with a RGB array for each color. Each such array has the same horizontal and vertical dimensions as the source image. Positions where the color in question has been detected contain the original RGB color found in the source image; other positions have the value 0 (black), except in the case of the "achro(matic)" color, where they have the value 1 (white).>>> dict_rgb_arrays = color_extraction.get_rgb_arrays(img)

>>> for color in dict_rgb_arrays:
... matplotlib.image.imsave(color, dict_rgb_arrays[color])Using the following image as input:Original imagewhiteredorangeyellowgreencyanbluepurplepinkachromaticSimilarly toget_bool_arrays, it is possible to use a median filter and/or your own color definition set, with the same parameters (median_filterandcolor_def_path).Pixel countsThe functionget_countsreturns a dictionary with the number of pixels of each colour.>>> color_extraction.get_counts(img)
{'purple': 25, 'blue': 6652, 'achro': 2477505, 'cyan': 764, 'white': 9567, 'green': 185, 'red': 114555, 'pink': 163, 'orange': 150263, 'yellow': 5121}Similarly toget_bool_arrays, it is possible to use your own color definition set, with the same parameter (color_def_path). The median filter is not available for this function.Dependenciesscipy.cluster.vqskimage.filtersnumpyAuthorsChristelle Cocco- initial implementationRaphaÃ«l CerÃ©- contributorAris Xanthos- refactoringCreditsThis module was partially funded by the the Swiss National Science Foundation (SNSF), grant NÂ° CR11I1_156383.The current version (0.1a4) was implemented by Aris Xanthos based on the original code by Christelle Cocco availablehere.To cite:Cocco, C., CerÃ©, R., Xanthos, A., Brandt, P.-Y. 2019.Identification and quantification of colours in children's drawings. Workshop on Computational Methods in the Humanities 2018. pp. 11-21. Vol. 2314. CEUR Workshop ProceedingsLicenseThis project is licensed under the GNU General Public License v3 - see theLICENSEfile for details.AcknowledgementsWitold KupÅ›,mihirm3hub