This release is a pre-release and may not be stable for production use.
Color-extraction
Color-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:
| Function | Values of returned dict |
|---|---|
get_bool_arrays |
boolean ndarrays (1 per color) |
get_rgb_arrays |
RGB ndarrays (1 per color) |
get_counts |
integer counts of pixels (1 per color) |
Installing
>>> pip install color_extraction
Usage examples
A 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 at https://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 arrays
The function get_bool_arrays returns 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 a mask for 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 image | white | red | orange |
|---|---|---|---|
| yellow | green | cyan | blue |
| purple | pink | achromatic | |
It 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 arrays
The function get_rgb_arrays returns 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 image | white | red | orange |
|---|---|---|---|
| yellow | green | cyan | blue |
| purple | pink | achromatic | |
Similarly to get_bool_arrays, it is possible to use a median filter and/or your own color definition set, with the same parameters (median_filter and color_def_path).
Pixel counts
The function get_counts returns 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 to get_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.
Dependencies
- scipy.cluster.vq
- skimage.filters
- numpy
Authors
- Christelle Cocco - initial implementation
- Raphaël Ceré - contributor
- Aris Xanthos - refactoring
Credits
This 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 available here.
License
This project is licensed under the GNU General Public License v3 - see the LICENSE file for details.
Acknowledgements
Metadata
Release files for color-extraction 0.1a4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| color_extraction-0.1a4.tar.gz | 8.6 kB | Without provenance | Details for color_extraction-0.1a4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| color_extraction-0.1a4-py3-none-any.whl | Python 3 | none | any | Details for color_extraction-0.1a4-py3-none-any.whl |
Total release size: 29.7 kB
Release files / color_extraction-0.1a4.tar.gz
| Download URL | color_extraction-0.1a4.tar.gz |
|---|---|
| Size | 8.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e4f8d24bf2dcb649419515d3aca12db64850078f0a49c14a5f6804b123fcb7bd
|
|
BLAKE2b-256 checksum How to use checksums |
e3cba0b72e2a85ee4509e373023670d5ca625d1b1c98ce5db2a9f4e21039ad18
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
|
Release files / color_extraction-0.1a4-py3-none-any.whl
| Download URL | color_extraction-0.1a4-py3-none-any.whl |
|---|---|
| Size | 21.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b14b089e0b94b18bd2453a1ddfadf17b6536aa6253af893173e39dc572984eb1
|
|
BLAKE2b-256 checksum How to use checksums |
59378d8e3fe1a00f59ff7e01ef5c686665c51da04410bbd5092259be3711dc3b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
|
Security for color-extraction 0.1a4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.