ColorTools
ColorsFromImage
extract the colors from an image
Calling Sequence
Parameters
Options
Description
Examples
Compatibility
ColorsFromImage(image, opts)
image
-
string that is the name of an image file or an image array
opts
(optional) equation(s) of the form option=value where option is one of number, scale, space, or threshold
number= positive integer - limit on how many colors to process
scale= true, false, or non-negative number. If false the image will not be scaled before pixels are extracted into colors. If a number that number will be used for scaling.
space=string - color space of the returned colors or "hex"
threshold=positive float - a percent threshold for removing infrequently appearing colors
This command extracts the unique colors from an image file into a list of Color objects or hex strings. The image is usually scaled also so that its maximum dimension in pixels is no more than the square root of number (which is 1000 by default). Use scale=false to have every pixel in the original image processed--note that this may cause the command to be very slow and is not recommended for very large images but is recommended when you wish to recover the exact colors from a lossless image (png or gif for example) without risk of getting additional colors that may be created as artifacts of the scaling.
At most number colors will be returned. If there are more than number unique colors in the image they will be returned in order of how frequently they appear. This command will not choose arbitrarily among colors of the same frequency, but will either remove or return all colors of any given frequency.
If threshold option is given, then only colors that appear in more than that percent of pixels will be returned. No more than number colors will be returned and at least one color will be returned.
This command does not attempt to combine similar colors. To process the colors to reduce to the distinct shades use the ColorTools[ReducePalette] command.
with⁡ColorTools:
imgfile≔cat⁡kernelopts⁡datadir,/images/tree.jpg:
ImageTools:-Embed⁡ImageTools:-Read⁡imgfile
full≔ColorsFromImage⁡imgfile:
numelems⁡full
119
full50..75
〈RGB : 0.525 0.682 0.82〉,〈RGB : 0.486 0.659 0.804〉,〈RGB : 0.71 0.745 0.78〉,〈RGB : 0.325 0.408 0.153〉,〈RGB : 0.976 0.957 0.933〉,〈RGB : 0.541 0.694 0.824〉,〈RGB : 0.992 0.976 0.941〉,〈RGB : 0.49 0.655 0.804〉,〈RGB : 0.322 0.427 0.165〉,〈RGB : 0.808 0.808 0.816〉,〈RGB : 0.576 0.659 0.733〉,〈RGB : 0.722 0.757 0.792〉,〈RGB : 0.984 0.973 0.945〉,〈RGB : 0.729 0.765 0.8〉,〈RGB : 0.275 0.361 0.11〉,〈RGB : 0.467 0.639 0.784〉,〈RGB : 0.537 0.694 0.831〉,〈RGB : 0.569 0.706 0.816〉,〈RGB : 0.851 0.847 0.831〉,〈RGB : 0.494 0.667 0.804〉,〈RGB : 0.855 0.851 0.843〉,〈RGB : 0.961 0.941 0.925〉,〈RGB : 0.506 0.678 0.816〉,〈RGB : 0.275 0.353 0.106〉,〈RGB : 0.729 0.757 0.796〉,〈RGB : 0.533 0.69 0.827〉
more≔ColorsFromImage⁡imgfile,scale=0.11,number=1200:
numelems⁡more
1107
ReducePalette⁡more
〈Lab : 66.5 -4.95 -23.7〉,〈Lab : 4.89 -6.07 4.74〉,〈Lab : 64.6 -2.04 66.5〉,〈Lab : 33.8 -23.7 35.5〉,〈Lab : 96.2 0.263 4.16〉,〈Lab : 51.5 -7.47 7.88〉,〈Lab : 17.8 -16.2 19.5〉,〈Lab : 48.3 -14.3 46.8〉,〈Lab : 72.4 -2.5 1.42〉,〈Lab : 33.6 1.35 35.3〉,〈Lab : 27.8 -3.61 1.01〉,〈Lab : 69.2 -11.1 24.5〉,〈Lab : 56.3 -8.92 -5.41〉,〈Lab : 82.3 0.00503 -1.76〉,〈Lab : 41.5 -7.16 5.24〉,〈Lab : 59.9 -17.5 57.2〉,〈Lab : 27.7 -13.5 17.4〉,〈Lab : 54.4 -1.68 46.2〉,〈Lab : 57.8 -0.395 2.26〉,〈Lab : 72.5 -1.49 -7.81〉
Setting infolevel for ColorTools to 2 or greater will give useful information.
infolevelColorTools≔2
img≔ImageTools:-Read⁡imgfile
thresh≔ColorsFromImage⁡img,threshold=0.003
ColorsFromImage: processing a 266 by 400 pixel image
ColorsFromImage: scaling factor=0.111803
ColorsFromImage: scaled to a 30 by 45 pixel image
ColorsFromImage: found 1350 colors total
ColorsFromImage: found 1174 unique colors
ColorsFromImage: returning the 14 colors that appear at least 4 times
thresh≔〈RGB : 0.267 0.384 0.0941〉,〈RGB : 0.498 0.667 0.804〉,〈RGB : 0.467 0.643 0.796〉,〈RGB : 0.502 0.671 0.808〉,〈RGB : 0.486 0.659 0.804〉,〈RGB : 0.541 0.694 0.824〉,〈RGB : 0.49 0.655 0.804〉,〈RGB : 0.576 0.659 0.733〉,〈RGB : 0.514 0.671 0.812〉,〈RGB : 0.51 0.678 0.816〉,〈RGB : 0.984 0.973 0.937〉,〈RGB : 0.984 0.969 0.933〉,〈RGB : 0.482 0.655 0.8〉,〈RGB : 0.988 0.976 0.941〉
The ColorTools[ColorsFromImage] command was introduced in Maple 18.
For more information on Maple 18 changes, see Updates in Maple 18.
The ColorTools[ColorsFromImage] command was updated in Maple 2019.
The image parameter was updated in Maple 2019.
The scale and threshold options were introduced in Maple 2019.
For more information on Maple 2019 changes, see Updates in Maple 2019.
See Also
ColorTools/ColorSpaces
ColorTools:-Color
ImageTools
Download Help Document