Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial (non-profit) use, ONLY.

    For use of my scripts in commercial (for-profit) environments or non-free applications, please contact me (Fred Weinhaus) for licensing arrangements. My email address is fmw at alink dot net.

    If you: 1) redistribute, 2) incorporate any of these scripts into other free applications or 3) reprogram them in another scripting language, then you must contact me for permission, especially if the result might be used in a commercial or for-profit environment.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

    Please read the Pointers For Use on my home page to properly install and customize my scripts.

DOMINANTCOLOR


Computes the dominant color in an image.

Download Script

last modified: December 18, 2021



USAGE: dominantcolor [-m mode] [-n numcolors] [-e excludecolor] [-f fuzzval] [-p print] [-s swatches] infile
USAGE: dominantcolor [-h or -help]

-m ... mode ........... mode of processing; options are: 1=unrestricted
....................... colors, 2=exclude black, 3=exclude white, 4=exclude
....................... any color, 5=only high saturation colors, 6=only
....................... high saturation and brightness colors; default=1
-n ... numcolors ...... number of colors to consider during color reduction;
....................... integer>1; default=6
-e ... excludecolor ... exclude color to be ignored in getting dominant
....................... color; any valid opaque IM color; default=black
-f ... fuzzval ........ fuzz value for modes 2-6; smaller values select
....................... colors closer to the mode desired, but may lead
....................... to less than the selected numcolors; 0<=integer<=100;
....................... default=50
-p ... print .......... print options: all (hexcolors and counts for all
....................... numcolors) or dominant (only the hexcolor for the
....................... dominant color; default=dominant
-s ... swatches ....... view or save swatches for option -p print; view or save;
....................... default is no swatches

PURPOSE: To find the dominant color in an image.

DESCRIPTION: Finds the dominant color or selected number of colors in an image. Modifiers can be used to restrict colors in the image. The alpha channel will be disabled for this script.

-m mode ... MODE of processing. The options are: 1=unrestricted colors, 2=exclude black, 3=exclude white, 4=exclude any color, 5=only high saturation colors, 6=only high saturation and brightness colors. The default=1.

-n numcolors ... NUMCOLORS is the number of colors to consider during color reduction. Values are integers>1. The default=6.

-e excludecolor ... EXCLUDECOLOR is an exclude color to be ignored in getting the dominant color. Any valid opaque IM color. The default=black for -m 4; otherwise, not used.

-f fuzzval ... FUZZVAL is the fuzz value for modes 2-6. Smaller values select colors closer to the mode desired, but may lead to less than the selected numcolors. Values are 0<=integers<=100; The default=50.

-p print ... PRINT options: all (hexcolors and counts for all numcolors) or dominant (only the hexcolor for the dominant color. The default=dominant.

-s swatches ... view or save SWATCHES for option -p print. Choices are: view (v) or save (s). The default is no swatches. If save, then the output will be named with either _dominantcolor or _swatches appended to the input name as a gif image.

CAVEAT: No guarantee that this script will work on all platforms, nor that trapping of inconsistent parameters is complete and foolproof. Use At Your Own Risk.


EXAMPLES


Example 1

Original Image
(source)

Arguments

Swatches
(most frequent on left)

Counts vs Hexcolor

-m 1 -n 6 -p all -s save

1043 #3D3F39
589 #6F798E
317 #98AAC1
234 #6D8EB0
203 #8C7B55
114 #236295

-m 2 -n 6 -f 50 -p all -s save

821 #737D91
726 #8C919D
336 #658FB1
276 #A58862
203 #A8C9E8
138 #287DB8

-m 6 -n 6 -f 50 -p all -s save

592 #466C94
587 #2D5E8C
494 #5AA1D1
445 #86663E
195 #728738
187 #A48C48

-m 6 -n 6 -f 50 -p dominant -s save

#466C94



Example 2
(source)

Original Image

Arguments

Swatches
(most frequent on left)

Counts vs Hexcolor

-m 1 -n 6 -p all -s save

2352 #FFFFFF
53 #32AFE6
33 #D5DDDD
31 #B0C8D2
21 #494846
10 #AB7F52

-m 3 -n 6 -f 50 -p all -s save

922 #24A1E2
877 #18B2EA
577 #6A6A69
110 #473E35
14 #AA7A4F

-m 3 -n 6 -f 50 -p dominant -s save

#24A1E2



Example 3

Original Image
(source)

Arguments

Swatches
(most frequent on left)

Counts vs Hexcolor

-m 1 -n 6 -p all -s save

622 #1B2505
560 #334B06
530 #526605
401 #B92F2B
262 #BBAAA8
125 #664340

-m 2 -n 6 -f 30 -p all -s save

1009 #556902
528 #B3312B
333 #BAAFAA
254 #626052
234 #BB8A6B
142 #676B07

-m 2 -n 6 -f 40 -p all -s save

667 #E62F0C
493 #B8B0A9
372 #807D02
363 #AC8C6C
362 #C4483A
243 #757164

-m 3 -n 6 -f 40 -p dominant -s save

#E62F0C



What the script does is as follows:

  • Scales the image to 50x50 size
  • Optionally filters out certain colors by making them transparent
  • Uses -sparse-color to filter out only non-transparent pixels
  • Reduces the number of colors in the image using -colors
  • Gets the histogram and sorts on most frequent colors first
  • Lists the counts and hexcolors
  • Optionally displays the swatch of the found colors

See the script for more details