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.

COLORCOORDS


Locates in the image the first location and/or last location for the closest pixel within the fuzz value to the color specified.

Download Script

last modified: December 15, 2018



USAGE: colorcoords [-c color] [-m mode] [-f fuzzval] infile
USAGE: colorcoords [-h or -help]

-c ... color ..... Any valid IM color specification; default=white
-m ... mode ...... Output coordinate location mode: choices are:
.................. first (f) or last (l) or both (b);
.................. default=first
-f ... fuzzval ... Fuzz value in percent for locating nearest color;
.................. 0<=float<=100; default=0

PURPOSE: To locate in the image the first location and/or last location for the closest pixel within the fuzz value to the color specified

DESCRIPTION: COLORCOORDS locates in the image the first location and/or last location for the closest pixel within the fuzz value to the color specified.

ARGUMENTS:

-c color ... COLOR is any valid IM color specification, including RGB, HEX, HSL, HSB, CMYK or by name. If not a color name, the color specification should be enclosed in quotes.

-m mode ... MODE is the output coordinate location mode: choices are: first (f) or last (l) or both (b). The default=first.

-f fuzzval ... FUZZVAL is the fuzz value in percent for locating nearest color. Values must be floats between 0 and 100. The default=0

NOTE: Prior to IM 6.5.6-6, HSL/HSB colors may not work correctly, as changes and bugs were fixed starting with IM 6.5.6-4. Prior to IM 6.5.6-4, HSL colors were specified only with hue in range 0-360 and saturation and lightness as percentages. HSB color specification and swatches were only first available and correct starting with IM 6.5.6-6. For current color specifications, see http://www.imagemagick.org/script/color.php

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


Original Image

Arguments

Coordinates

Image With Coordinates Marked
(see green circles)

-c "rgb(234,43,44)" -m both -f 0

First Location = 70,23
Last Location = 154,225

Arguments

Coordinates

Image With Coordinates Marked
(see green circles)

-c "rgb(234,43,44)" -m both -f 15

First Location = 79,20
Last Location = 221,230


What the script does is as follows:

  • Use compare with a 1x1 pixel of the given color to find the
    first location
  • Rotate the image 180 degrees and use compare with a 1x1 pixel
    of the given color to find the last location

This is equivalent to the following IM commands:

  • ww=`convert $tmpA1 -format "%w" info:`
  • hh=`convert $tmpA1 -format "%h" info:`
  • data=`compare -metric AE $fuzzing $searching $tmpA1 \
    \( -size 1x1 xc:"$color" \) null: 2>&1 |\
    tr -cs ".0-9\n" " "`
  • x=`echo "$data" | cut -d\ -f2`
  • y=`echo "$data" | cut -d\ -f3`
  • echo "First Location = $x,$y"
  • convert $tmpA1 -rotate 180 $tmpA1
  • data=`compare -metric AE $fuzzing $searching $tmpA1 \
    \( -size 1x1 xc:"$color" \) null: 2>&1 |\
    tr -cs ".0-9\n" " "`
  • x=`echo "$data" | cut -d\ -f2`
  • y=`echo "$data" | cut -d\ -f3`
  • echo "Last Location = $((ww-1-x)),$((hh-1-y))"